Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

515
Visualizações
django how to create field dependent field

I have a post model in which i have post type field. I want that when user select post type = assignment this it ask for submission deadline other wise it does not ask anything. and how can i display it in template.

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})
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

In the model you need to add a clean method for fields that depend on each other. https://docs.djangoproject.com/en/1.11/ref/models/instances/#django.db.models.Model.clean

In the model Admin you need to add some JavaScript to show and hide the field. https://docs.djangoproject.com/en/1.11/ref/contrib/admin/#modeladmin-asset-definitions

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda