P
P
pvgdrk2013-12-14 12:00:58
Django
pvgdrk, 2013-12-14 12:00:58

django models. How to make a reference in a model field to the value of a field of another model?

Model structure:

class Profile (models.Model):
    user = models.ForeignKey(MyUser)
    university = models.CharField(max_length=512)
    #...
    
class Stream (models.Model):
    #...

class Enty (models.Model):
    stream = models.ForeignKey(Stream)
    entry = (max_length=512)
    ...

The structure fully meets the tasks assigned to it, namely adding, editing and deleting records. It's all about the interaction of the user with the database.
But there is still the task of the administrator parsing Entry records in conjunction with the university field. That is, the administrator, through a special interface, makes a request to the Enty records on some streams, and for him the identity of the user and the relationship between his records does not matter. The subject of consideration is only the connection stream - entry - university.
i.e
class Enty (models.Model):
    stream = models.ForeignKey(Stream)
    entry = (max_length=512)
    university = #ссылка на Profile.university
    ...

is it possible to make this link?
Yes, you can make the university field in the Entry a text field and each time you create an Enty record, write the university value from the Profile there, but this is somehow not cool at all.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alz, 2013-12-14
@alz

It's not very clear what you want to reference in your schema if you have a one-to-many user - profile relationship. But even if the user field in the Profile would be OneToOneField, I’m not sure right off the bat that it’s possible to catch up with university from Profile in one request through the Dzhang orm. In pure SQL, one could write a query by joining Entry and Profile by user_id.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question