使用多个外键时Django admin下拉列表中的意外值

时间:2012-11-05 10:19:26

标签: django django-models django-admin django-south

以下是我的models.py。问题是,在管理面板中,User模型中的Ingredience列指向Ingredience Category值,而不是User值。

Ingredience模型中,我有 2个外键。我不确定这是不是问题。

来自管理面板的屏幕截图:

enter image description here

Models.py:

from django.db import models
from django.contrib.auth.models import User

class IngredienceCategory(models.Model):
    name = models.CharField(max_length=30)
    user = models.ForeignKey(User, null=True, blank=True)

    class Meta:
        verbose_name_plural = "Ingredience Categories"

    def __unicode__(self):
        return self.name


class Ingredience(models.Model):
    name = models.CharField(max_length=30)
    category = models.ForeignKey(IngredienceCategory, null=True, blank=True)
    user = models.ForeignKey(User, null=True, blank=True)

    class Meta:
         verbose_name_plural = "Ingredients"

    def __unicode__(self):
        return self.name


class Food(models.Model):
    name = models.CharField(max_length=30)
    ingredients = models.ManyToManyField(Ingredience)
    html_id = models.CharField(max_length=30, null=True, blank=True)
    user = models.ForeignKey(User, null=True, blank=True)

    class Meta:
        verbose_name_plural = "Foods"

    def __unicode__(self):
        return self.name

任何想法这里的问题是什么?以下是我在出现此问题之前采取的步骤:

  • 我在所有3个模型中添加了user列并运行了南迁移
  • 之后,我手动将值插入所有3个表中的用户列
  • 模型IngredienceCategoryFood正常工作(即在User下拉列表中显示用户列表
  • 这是SQL:http://pastebin.com/3mHpXA3F

修改 要添加的东西(可能有助于解决此问题):我注意到单击管理面板中“成分模型”中“用户”列旁边的加号(+),将显示一个用于添加新用户的弹出窗体,这是正确的。

编辑2: admin.py文件的相关部分:

class IngredienceAdmin(admin.ModelAdmin):
    search_fields = ('name',)
    list_filter = ('category',)
    ordering = ('name',)

#    def formfield_for_foreignkey(self, db_field, request, **kwargs):
#        #if db_field.name == 'ingrediencecategory':
#        kwargs['queryset'] = IngredienceCategory.objects.order_by('name')
#        return super(IngredienceAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == 'ingrediencecategory':
            kwargs['queryset'] = IngredienceCategory.objects.order_by('name')
            return super(IngredienceAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

        if db_field.name == 'user':
            kwargs['queryset'] = User.objects.order_by('name')
            return super(IngredienceAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

2 个答案:

答案 0 :(得分:0)

您的管理模型是什么样的?

你的模特看起来很好,我不明白为什么你会遇到这个问题。你可以在一个模型中拥有多个外键,这是正常的。

我把它们扔进了一个项目来看一看(用一个空白的管理模型),我无法重现这个。

答案 1 :(得分:0)

回答我自己的问题。如果有人遇到类似的问题,这是工作解决方案(admin.py):

class IngredienceAdmin(admin.ModelAdmin):
    search_fields = ('name',)
    list_filter = ('category',)
    ordering = ('name',)

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == 'ingrediencecategory':
            kwargs['queryset'] = IngredienceCategory.objects.order_by('name')

        if db_field.name == 'user':
            kwargs['queryset'] = User.objects.order_by('username')

        return super(IngredienceAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)