Django内联表单新添加了外键字段,但未显示在新添加的内联行中

发布于 2024-10-13 19:58:25 字数 1954 浏览 1 评论 0原文

我是 Django 世界的新手。我的问题是我有两个模型,如下所示。它与 Grapelli 和内联排序器配合得很好。唯一的问题是每当我为“设备”或“图像类型”字段添加新的外键时。它们不会显示在新添加的内联行的下拉菜单中。我浏览了互联网,但找不到类似的问题和解决方案。 我希望得到一些帮助。

我的模型是:

from django.db import models
from datetime import datetime
from thumbs import ImageWithThumbsField
from positions.fields import PositionField 
class Artist(models.Model):
name = models.CharField(max_length=55)
   def __unicode__(self):
       return self.name

class ImageType(models.Model):
   name = models.CharField(max_length=55)
   def __unicode__(self):
       return self.name

class Equipment(models.Model):
   name = models.CharField(max_length=55)
   def __unicode__(self):
       return self.name

class Image(models.Model):
   name = models.CharField(max_length=255)
   image_file = models.ImageField(upload_to = "images/%Y-%m-%d")
   Image_Type = models.ForeignKey(ImageType)
   upload_date = models.DateTimeField('date_published',default=datetime.now)
   artist = models.ForeignKey(Artist)
   equipment = models.ForeignKey(Equipment)
   order = PositionField(collection='artist')
   def __unicode__(self):
       return self.name

   class Meta:
       ordering = ['order']

我的 admin.py 是:

from gallery.models import Image,ImageType,Artist,Equipment
from django.contrib import admin

class ImageUploadAdmin(admin.ModelAdmin):
   fields = ['name','artist','equipment','image_file','Image_Type','upload_date']
   list_filter = ['upload_date']
   date_hierarchy = 'upload_date'

class ImageInline(admin.TabularInline):
   model = Image
   list_display = ('name','equipment','image_file','Image_Type','upload_date')
   sortable_field_name = "order"
   exclude = ('upload_date',)
   extra = 0

class ArtistAdmin(admin.ModelAdmin):
   inlines = [
       ImageInline,
   ]

admin.site.register(Artist,ArtistAdmin)
admin.site.register(Image, ImageUploadAdmin)
admin.site.register(ImageType) 
admin.site.register(Equipment)

I am quite newbie in Django world. My question is I ve two models shown below. It works quite well with Grapelli and inline-sortables. Only problem is whenever I add a new foreign key for "equipment" or "image type" fields. They don't show up in the drop down menu of newly added inline rows. I went through internet but couldn't find a smilar problem and a solution.
I would appreciate some help with this.

My model is:

from django.db import models
from datetime import datetime
from thumbs import ImageWithThumbsField
from positions.fields import PositionField 
class Artist(models.Model):
name = models.CharField(max_length=55)
   def __unicode__(self):
       return self.name

class ImageType(models.Model):
   name = models.CharField(max_length=55)
   def __unicode__(self):
       return self.name

class Equipment(models.Model):
   name = models.CharField(max_length=55)
   def __unicode__(self):
       return self.name

class Image(models.Model):
   name = models.CharField(max_length=255)
   image_file = models.ImageField(upload_to = "images/%Y-%m-%d")
   Image_Type = models.ForeignKey(ImageType)
   upload_date = models.DateTimeField('date_published',default=datetime.now)
   artist = models.ForeignKey(Artist)
   equipment = models.ForeignKey(Equipment)
   order = PositionField(collection='artist')
   def __unicode__(self):
       return self.name

   class Meta:
       ordering = ['order']

And My admin.py is:

from gallery.models import Image,ImageType,Artist,Equipment
from django.contrib import admin

class ImageUploadAdmin(admin.ModelAdmin):
   fields = ['name','artist','equipment','image_file','Image_Type','upload_date']
   list_filter = ['upload_date']
   date_hierarchy = 'upload_date'

class ImageInline(admin.TabularInline):
   model = Image
   list_display = ('name','equipment','image_file','Image_Type','upload_date')
   sortable_field_name = "order"
   exclude = ('upload_date',)
   extra = 0

class ArtistAdmin(admin.ModelAdmin):
   inlines = [
       ImageInline,
   ]

admin.site.register(Artist,ArtistAdmin)
admin.site.register(Image, ImageUploadAdmin)
admin.site.register(ImageType) 
admin.site.register(Equipment)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文