Django Foreignkey将选择限制为来自另一个模型的值

时间:2014-09-11 21:45:10

标签: python django

我有几个型号类别类型类型有一个外键到类别

类别模型包含(人员,位置,设备......)等记录,类型模型包含(员工,经理,客户,供应商,总部)等记录地址,客户地址,供应商地址,切片机,搅拌机,烤箱...)

我有另一个位置模型和一个定义位置类型的字段,我设置了类型模型的外键并且工作正常。我想要它做的是当我进入管理位置表单并单击类型列表我只希望它显示位置类型,例如(主办公室地址,客户地址,供应商地址)

我尝试过 type = models.ForeignKey(Type,limit_choices_to = {&#39; category_id&#39;:4}),此列表只是地点类型,这就是我想要的。< / p>

我的问题是我不想硬编码 category_id of 4 ,因为这可能会改变,具体取决于类别模型中的ID。 这是我的代码

======================== categories.py ================= ======

from django.db import models


class CategoryQueryset(models.query.QuerySet):
    def location_category(self):
        return self.filter(category='location')


class CategoryManager(models.Manager):
    def get_queryset(self):
        return CategoryQueryset(self.model, using=self._db)

    def location_category(self):
        return self.get_queryset().location_category()


class TypeQueryset(models.query.QuerySet):
    def active(self):
        return self.filter(active=True)

    def location_type(self):
        return self.filter(category=Category.objects.location_category())


class TypeManager(models.Manager):
    def get_queryset(self):
        return TypeQueryset(self.model, using=self._db)

    def active(self):
        return self.get_queryset().active()

    def location_type(self):
        return self.get_queryset().location_type()


class Category(models.Model):
    category = models.CharField(unique=True, max_length=30)
    description = models.CharField(max_length=200, blank=True)
    locked = models.BooleanField(default=False)

    objects = CategoryManager()

    def __str__(self):
        return self.category+ ": " + self.description

    class Meta:
        ordering = ['category']



class Type(models.Model):
    category= models.ForeignKey(Category)
    type = models.CharField(unique=True, max_length=30)
    description = models.CharField(max_length=200, blank=True, null=True)
    sort_order = models.DecimalField(max_digits=8, decimal_places=2)
    active = models.BooleanField(default=True)
    locked = models.BooleanField(default=False)

    objects = TypeManager()

    def __str__(self):
        return self.type + ": " + self.description

    class Meta:
        ordering = ['type']

=========================== locations.py ============== ==============

from django.db import models
from categories.models import Type


class LocationQueryset(models.query.QuerySet):
    def active(self):
        return self.filter(active=True)


class LocationManager(models.Manager):
    def get_queryset(self):
        return LocationQueryset(self.model, using=self._db)

    def active(self):
        return self.get_queryset().active()


class Location(models.Model):
    # This is where I am having an issue, I would like to make the 4 a lookup object.
    # something like {'category_id':Category.objects.location_category()[0].id} 
    type = models.ForeignKey(Type,limit_choices_to={'category_id': 4})

    parent_location_id = models.ForeignKey('self', blank=True, null=True)
    name = models.CharField(max_length=25)
    address = models.CharField(max_length=50, blank=True, null=True)
    city = models.CharField(max_length=25, blank=True, null=True)
    state = models.CharField(max_length=2, blank=True, null=True)
    zip = models.CharField(max_length=10, blank=True, null=True)
    lat = models.FloatField(blank=True, null=True)
    lon = models.FloatField(blank=True, null=True)
    cross_road_1 = models.CharField(max_length=50, blank=True, null=True)
    cross_road_2 = models.CharField(max_length=50, blank=True, null=True)
    map_link = models.URLField(max_length=200, blank=True, null=True)
    directions = models.TextField(blank=True, null=True)
    size = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)
    active = models.BooleanField(default=True)
    created = models.DateTimeField(blank=True, null=True, auto_now=False, auto_now_add=True)
    updated = models.DateTimeField(blank=True, null=True, auto_now=True, auto_now_add=False)

    objects = LocationManager()

    def __str__(self):
        return self.name + ": " + self.address + ": " + self.city + ": " + self.state

    class Meta:
        ordering = ['name']

我试图找出一种方法,使4的值成为类别模型中的值,例如 {&#39; category_id&#39;:Category.objects.location_category( )[0] .ID} 但这并不起作用我得到了一个&#34; RuntimeError:App注册表还没有准备好。&#34; 我想知道这是否是正确的方法,或者是否有更好的方法。

非常感谢任何帮助。

0 个答案:

没有答案