B
B
BotaniQ_Q2017-12-24 20:26:26
C++ / C#
BotaniQ_Q, 2017-12-24 20:26:26

How to pass an object with a ForeignKey to Django views?

Let’s say I have views with the add_comment function (I didn’t think of a better idea for the example), but in the Comment model name = ForeignKey(Name)
, if it was without ForeignKey it would be like this
:
exam = Exam.objects.create(name = "hohohoh" , value="33223"),
but how to make such a function that would
exam = Exam.objects.create(name = "hohohoh", value="33223", AND HERE is what is passed through Foreign Key)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2019-10-16
@GavriKos

Why not make it static then?
Singleton is not the only method of interaction between classes (and in general this is not about interaction, but oh well, the essence is clear). Read about dependency injection (without frameworks), for example.
In the same unit, you can use SerializeField, GetComponent, and the hated FindObjectOfType to the heap,

R
Rostislav Grigoriev, 2017-12-24
@crazyzubr

instance_name = Name.objects.first()

Exam.objects.create(name = "hohohoh", value="33223", name=instance_name)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question