简体   繁体   中英

show parent in child screen django admin python

I'm new to python django and we decide to develop an application with this framework for developing the admin we faced some issues here's a snippet of my code the models is like :

class Category(models.Model):
   name = models.CharField(max_length=60)
   description = models.CharField(max_length=100)

class Product(models.Model):
   name = models.CharField(max_length=60)
   description = models.CharField(max_length=100)
   category = models.ForeignKey(Category)

And in the corresponding tables in database table Product has a foreign-key reference to Category (category_id)

In admin.py file I have :

 class ProductAdmin(admin.ModelAdmin):
    fields = ['name','description'];

 class CategoryAdmin(admin.ModelAdmin):
     fieldsets = [
             (None,{'fields':['name']}),
             ('Description',{'fields' : ['description']})
             ]
   inlines = [ProductInline] 

When I need to insert a product into the database via the django admin panel there is no field to select the category and I received the following message :

"Column 'category_id' cannot be null"

where am I making mistake? how do I edit my code so that in admin panel it provides a dropdown or something to select its parent(In this case category)

Get rid of the "fields" option in your ProductAdmin. You might be confusing "fields" with "list_display"? Try

class ProductAdmin(admin.ModelAdmin):
    list_display = ['name','description']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM