我有一个邮政模型,其中我具有邮政类型字段。我希望当用户选择发布类型=分配时,要求提交截止日期其他明智的明智。以及如何在模板中显示它。
models.py
class Post(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
title = models.CharField(max_length=120)
slug = models.SlugField(unique=True, blank = True)
content = models.TextField()
choice = (
('post','post'),
('anouncement','anouncement'),
('question', 'question'),
('assignment', 'assignment')
)
post_type = models.CharField(choices = choice, default = 'post', max_length = 12)
classroom = models.ForeignKey(Classroom)
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
def __unicode__(self):
return self.title
def __str__(self):
return self.title
@property
def comments(self):
instance = self
qs = Comment.objects.filter_by_instance(instance)
return qs
@property
def get_content_type(self):
instance = self
content_type = ContentType.objects.get_for_model(instance.__class__)
return content_type
def get_absolute_url(self):
return reverse("posts:detail", kwargs={"slug": self.slug})
在模型中,您需要为彼此依赖的字段添加一种干净的方法。 https://docs.djangoproject.com/en/1.11/ref/models/instances/#django.db.models.model.model.clean.clean
在模型管理员中,您需要添加一些JavaScript以显示和隐藏字段。https://docs.djangoproject.com/en/1.11/ref/contrib/admin/#modeladmin-asset-definitions