简体   繁体   中英

Django, listing the query-set of connected Models

I would like to list all conversations of a User. As seen from picture below, Conversation Model is not Directly connected to the User table. This is a freelancer site project, where users can register as workers to do jobs posted by users.

The result I want is:

my_conversations = User.Conversation.all()

I'm adding this picture, since it paints everything that I will summarise below. 在此处输入图片说明

Let's images I have 2 users, one of which is a registered worker. Then it looks as follows:

**Users**
id   Name   ...
1    John
2    Bob

**Worker**
id   user_id   ...
1    2

John posted some job. We add 1 new entry to the JOB table.

**Job**
id    title                          user_id
1     Help me design a database      1

This is a M-to-M relationship (worker can apply for many jobs and one job can have many workers), we define a new table where job_has_worker .

For each job_has_worker , there should be a way to communicate between worker and job poster.

Now what I would like Django to do is load all conversations the user who is logged in eg load a list of my conversations for the jobs I posted.

Views - My Posted Jobs

User = request.user
Jobs = User.job_set.all()

Views - My Conversations, This is what I need

User = request.user
Jobs = User.job_set.all()
for job in Jobs:
    # load conversations
    converstation = job.converstation_set.all()
    # also somehow append this to all conversations

Views - Messages First I need to get conversation id

Messages = Conversation.get(pk=pk).message_set.get_all()

I'm going to add some security with if loops eg

if request.user.id not in message.sender || message.receiver
   return false

EDIT:


Models.py

class Worker(models.Model):
     user = OneToOneField(User)

class Conversation(models.Model):
     #What to here?
     #Job_Has_Worker = ForeignKey(Job, Worker)

class Message(models.Model):
     body = models.TextField(max_length=500, blank=False)
     sender = models.IntegerField()
     receiver = models.IntegerField()
     conversation = models.ForeignKey(Conversation)

class Job(models.Model):
    title = models.CharField(max_length=100, blank=False)
    description = models.TextField(max_length=500, blank=False)
    workers = models.ManyToManyField(Worker)

Without the relationship fields that involve Conversation I can't completely answer this, but here's an example using Job :

# All jobs for a given user:
Jobs.objects.filter(workers__user=request.user)

Your listed example of User.job_set.all() won't work, User doesn't have a direct relation to Job so it won't have a job_set attribute.

Assume Conversation is declared using a ForeignKey on the job/worker m2m implicit through model:

class Conversation(models.Model):
    job_worker = ForeignKey(Job.workers.through)

(Credit to How can I use the automatically created implicit through model class in Django in a ForeignKey field? for the syntax to reference the implicit through model.)

Then something like this should work to get conversations:

Conversation.objects.filter(job_worker__worker__user=request.user).distinct()

Or this to get messages:

Messages.objects.filter(conversation__job_worker__worker__user=request.user).distinct()

You can use the through argument to your ManyToManyField to get a nicer name for the intermediate model than Job.workers.through , of course.

You can also walk back from an instance, as you were heading towards with user.job_set.all() - it would have to be user.worker.job_set.all() to start, and you'd want to check to ensure that the user has a non-empty worker first, but because job_set is itself a manager that returns querysets of job s that's a bit more cumbersome to work with for this indirect a relation. some_worker.job_set.filter(...) is clearer than Job.objects.filter(worker=some_worker, ...) , though, so it depends on the circumstances.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM