我正在尝试使用limit_choices_to限制Django Admin对ForeignKey的选择,但我无法弄清楚如何正确地做到这一点.
如果类别id为16,这段代码可以实现我想要的,但我无法弄清楚如何使用当前类别id而不是硬编码.
class MovieCategory(models.Model):
category = models.ForeignKey(Category)
movie = models.ForeignKey(Movie)
prefix = models.ForeignKey('Prefix', limit_choices_to={'category_id': '16'},
blank=True, null=True)
number = models.DecimalField(verbose_name='Movie Number', max_digits=2,
blank=True, null=True, decimal_places=0)
Run Code Online (Sandbox Code Playgroud)
有可能以某种方式引用ForeignKey类别的id吗?
我有几个django模型看起来像这样:
from django.contrib.sites.models import Site
class Photo(models.Model):
title = models.CharField(max_length=100)
site = models.ForeignKey(Site)
file = models.ImageField(upload_to=get_site_profile_path)
def __unicode__(self):
return self.title
class Gallery(models.Model):
name = models.CharField(max_length=40)
site = models.ForeignKey(Site)
photos = models.ManyToManyField(Photo, limit_choices_to = {'site':name} )
def __unicode__(self):
return self.name
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用Gallery模型的各种乐趣limit_choices_to.我只希望管理员显示与此图库属于同一网站的照片的选项.这可能吗?