Access ForeignKey type from get_context_data using pk_url_kwargs

Viewed 24

I have a CreateView in which i want to access the type(choice field) of foreign key in my model which is being used in my current model.

Class Mymodel1(models.Model):
    name = models.CharField()
    type = models.CharField(choices=SomeTypes.choices())

    

Class MyModel2(models.Model):

    name = models.CharField()
    cas = models.ForeignKey(Mymodel1)


Class Mymodel2CreateView(models.Model):
    pk_url_kwargs = 'cas_id'

    def get_context_data(self):
        type = *queryset to access type from model1*
1 Answers

You want to access the field of ForeignKey before the object has been created. It does not seem possible, right? The thing you can do is to use SIGNALS with post_save and created.

Related