'django manytomany - get value from through
I have a manytomany field between 2 models:
class userProfile(models.Model):
boughtCoupons = models.ManyToManyField(Coupon, through='UserCoupons')
class UserCoupons(models.Model):
user = models.ForeignKey(userProfile)
coupon = models.ForeignKey(Coupon)
date = models.DateField()
class Coupon(models.Model):
name = models.CharField(max_length=10)
Now my problem is:
Given a coupon and a user ID, how can I get the date in which the user bought the coupon ?
Something like mycoupon.boutcoupons.filter(user=userid)
and then get the date somehow..
Problem is that the field belongs to the user... and if I access the field from the user I get a list of coupons anyway, without the date.
I just need the coupon-userID .date value.
Solution 1:[1]
Just query UserCoupons directly.
UserCoupons.objects.get(user=myuser, coupon=mycoupon)
or use the reverse relation from Coupon:
mycoupon.usercoupon_set.get(user=myuser)
Edit in response to comment There are two separate things going on here. Firstly, a ManyToMany does get added to the other side as a set (actually, a Manager) - using the name of the source model plus _set
. So in this case, Coupon
will have a userprofile_set
attribute.
Secondly, however, you'll see that's not what I used in my answer. That's because ForeignKeys also add reverse relation Managers to their target models. Your through model defines ForeignKeys to both userProfile and Coupon, so they both get usercoupons_set
attributes.
Solution 2:[2]
I just want to mention a point here. I know the question has already been answered by Daniel:
You can access the "date" attribute in the through the table using below code:
mycoupon.boutcoupons.filter(usercoupons__date="***")
Solution 3:[3]
I know this question is quite old however I am posting a easiest and straight answer to such problems as I myself struggled a lot in order to solve this issue and found no help from internet, so posting it to help many like me.
Models.py
------------------------
class userProfile(models.Model):
boughtCoupons = models.ManyToManyField(Coupon, through='UserCoupons')
class UserCoupons(models.Model):
user = models.ForeignKey(userProfile)
coupon = models.ForeignKey(Coupon)
date = models.DateField()
class Coupon(models.Model):
name = models.CharField(max_length=10)
View.py
-----------------
def result(request):
a = userProfile.objects.all()
b = Coupon.objects.all(name=a.boughtCoupons.all.0)
c = UserCoupons.objects.all(coupon=c.name.all.0)
context = {'a':a,'b':b,'c':c}
return render(request,'index.html',context)
intex.html
--------------------
*your Html declaration
* content
{%for dtl in c%}
<table>
<tr>
<th>coupon</th>
<th>UserID</th>
<th>Date</th>
</tr>
<tr>
<td>{{c.coupon}}</td>
<td>{{c.user}}</td>
<td>{{c.date}}</td>
</tr>
</table>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Ehsan Ahmadi |
Solution 3 | Rahul Mishra |