开发者

Django custome values for foreign keys

开发者 https://www.devze.com 2023-03-23 08:46 出处:网络
The thing I need to do seems quite simple, but I already spent quite a lot of time trying to solve it.... with no success.

The thing I need to do seems quite simple, but I already spent quite a lot of time trying to solve it.... with no success. So, I have a Template model defined:

class Template(models.Model):
    TYPE_CHOICES = (
                      ('campaign','Campaign email template'),
                      ('system','System email templa开发者_JS百科te'),
                      )
    Name = models.CharField(max_length=255)
    Type = models.CharField(max_length=255, verbose_name='Template type', choices=TYPE_CHOICES)
    Content  = models.TextField(max_length=3000)
    def __unicode__(self):
        return self.Name

And I have my System Email model defined like this:

class SystemEmail(models.Model):    
    Name = models.CharField(max_length=255)
    Template = models.ForeignKey(Template,null=True)
    Subject  = models.CharField(max_length=255,help_text='Shortcodes are allowed (see bottom of the page)')    
    Content  = models.TextField(max_length=3000,)

So the Template fields in my System email model is a foreign key which will be represented by a drop down. However, I do not wish ALL Template records to appear in that dropdown, but just the ones that have type='system'

So, how should I tell Django to do this?


limit_choices_to:

template = models.ForeignKey(Template, null=True, limit_choices_to={'type': 'system'})

(Please, use lower case names for your fields.)

0

精彩评论

暂无评论...
验证码 换一张
取 消