英文:
Django: Populate a model field from another model's field and make both amendable by admin
问题
我想创建一个数据库,其中管理员用户可以通过管理员门户添加产品。这些产品将具有一个类别字段。
我还希望管理员能够添加/删除类别类型。但是,我对Django相对陌生,不知道如何实现这一点。我觉得我需要使用外键(ForeignKey),但我对如何将其链接到特定的类别名称感到困惑。
我创建了一个Category模型和一个Product模型:
class Category(models.Model):
    created_on = models.DateTimeField(auto_now_add=True)
    category_image = CloudinaryField('image', default='placeholder')
    category_name = models.CharField(max_length=50, unique=True)
    def __str__(self):
        return self.category_name
class Product(models.Model):
    created_on = models.DateTimeField(auto_now_add=True)
    main_image = CloudinaryField('image', default='placeholder')
    item_name = models.CharField(max_length=50, unique=True)
    slug = models.SlugField(default="", null=False)
    price = models.DecimalField(max_digits=8, decimal_places=2)
    style = models.ForeignKey(Category, related_name='category_name', on_delete=models.CASCADE)
    likes = models.ManyToManyField(User, related_name='product', blank=True)
    def __str__(self):
        return self.item_name
我尝试链接Product模型中的style字段,使其从Category模型中的category_name字段获取数据。
这是否可能?我是否可以使其在管理员门户上显示为下拉菜单?非常感谢任何帮助!
英文:
I want to create a database where an admin user can add products via the admin portal. The products will have a category field.
I want the admin to also be able to add/remove category types. However I'm relatively new to django and can't work out how to do this. I feel like I need to use a ForeignKey but I'm getting confused about how I link it to the specific category name.
I created a Category model and a Product model:
class Category(models.Model):
    created_on = models.DateTimeField(auto_now_add=True)
    category_image = CloudinaryField('image', default='placeholder')
    category_name = models.CharField(max_length=50, unique=True)
    def __str__(self):
        return self.category_name
class Product(models.Model):
    created_on = models.DateTimeField(auto_now_add=True)
    main_image = CloudinaryField('image', default='placeholder')
    item_name = models.CharField(max_length=50, unique=True)
    slug = models.SlugField(default="", null=False)
    price = models.DecimalField(max_digits=8, decimal_places=2)
    style = models.ForeignKey(Category, related_name='category_name', on_delete=models.CASCADE)
    likes = models.ManyToManyField(User, related_name='product', blank=True)
    def __str__(self):
        return self.item_name
I've tried to link the style field in the Product model so that it pulls it's data from the category_name field in the Category model.
Is this possible? Can I get it to appear as a dropdown on the admin portal somehow? Any help much appreciated!
答案1
得分: 0
与关联到一个category_name不同,您将产品关联到了一个category类,该类将具有与category类相关的category_name和其他字段。
假设您获取了产品:
product = Product.objects.first()
product.style.category_name # 将获取产品名称
现在,要使这两个模型都出现在管理面板中,您需要在您的应用程序的your-app/admin.py文件中注册这些模型:
admin.site.register(Category)
admin.site.register(Product)
英文:
Rather then relating to a category_name, you have related the product a category class, which will have category_name and other fields related to the category class.
Let's say you get product
product = Product.objects.first()
product.style.category_name # will get you the product name
Now to get both models appear in admin panel then you have to register the models in the your-app/admin.py file of you app.
admin.site.register(Category)
admin.site.register(Product)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论