0

i want to make a notification function, and i need fields from 2 different models. how can i access those fields? in my notification view i wrote this

  data = Notices.objects.filter(last_login<date_follow)

where last_login belongs to the model class User , and date_follow to Follow but it is not a proper and correct way of accessing those variables. How can i access them? I need to compare the two dates for realising the notifications that one did not see since his last login.

Thanks!

1
  • Please post your model code for Notices and Follow so we can know more to be able to help. (Also, those aren't class variables - you're talking about variables belonging to objects, which are instances of the relevant classes.) Commented Jun 4, 2010 at 9:46

1 Answer 1

2

In general you want to join two tables. In Django this is best possible if you have a foreign key from one table to the other. You maybe want / have your models like this:

class User(models.Model):
  last_login = ...

class Notice(models.Model):
  ...

class Follow(models.Model):
  user = models.ForeignKey(User)
  notice = models.ForeignKey(Notice)
  date_follow = ...

and your query

Notice.objects.filter(follow_set__date_follow__gt = follow_set__user__last_login)

I don't have tested this query, but here 'follow_set' is automatically created by Django and is a Manager which returns the reverse set for the foreign key. If you want, you can use 'related_name' with your foreign key to choose another name here.

Sign up to request clarification or add additional context in comments.

1 Comment

well, i still get an error : global name 'relations_set__user__last_login' is not defined the error seems quite logical... were am i wrong? thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.