Now costs from all users are displayed, I need to display only those that belong to the Profile.
I have model:
class Spend(models.Model):
user = models.ForeignKey(Profile, null=True, blank=True, on_delete=models.SET_NULL)
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)
name = models.CharField(max_length=200)
spend = DecimalField(max_digits=14, decimal_places=2)
And model:
class Profile(models.Model):
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)
user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank=True)
username = models.CharField(max_length=200, blank=True, null=True)
In my views:
@login_required
def costs(request):
filter = CostFilter(request.GET, queryset=Spend.objects.all())
return render(request, 'users/includes/costs.html', {
'filter': filter
})
Template:
{% for cost_item in filter.qs %}
<tbody>
<tr>
<td>{{ cost_item.name }}</td>
<td>{{ cost_item.spend }}</td>
<td>
class CostFilter(django_filters.FilterSet):
class Meta:
model = Spend
fields = ("name", "spend")
I trying:
filter = CostFilter(request.GET, queryset=Spend.objects.filter(user=request.user)
But get:
Cannot query "test": Must be "Profile" instance.
I believe the issue is your foreign key. If I understand that what you want to do is access any Spend with the foreign key matching the logged in user's id, then you can do so via a "related name" which for some reason isn't mentioned in that many-to-one page in the django docs, since it's the only way I've ever learned how to do so.
Here's some code of mine doing so.
class Brand(models.Model):
name = models.CharField(max_length=50)
class Car(models.Model):
make = models.ForeignKey('Brand',on_delete=models.CASCADE,related_name='cars')
Then when I python manage.py shell assuming I already made "Honda" a brand:
$ from cars_and_brands.models import Car,Brand
$ c = Car(make=Brand.objects.get(name='Honda')
$ Brand.objects.get(name='Honda').cars.all()
Then that will return every car with the foreign key of Honda's Brand ID
I believe that by giving "Spend" a related_name='user_spends' or whatever then you could do something like
profile = Profile.objects.get(#query for logged in user)
profile.user_spends.all()
And then you'd get all the user_spends associated.
Hopefully I understood your question and that helps.
I believe the way the docs does it is by adding "_set" and has some default pattern it named the foreign key on the reverse side, but I learned SQL before I learned django models so I'd rather just be able to call it what I want and in my short time with Django so far I've always just used related_name since it's much more readable and easy to follow in my opinion.
Apparently you can run into issues if you don't have unique related names, maybe that's why they seem to be either neglected or discouraged in the docs.
Here are some docs I've referenced:
One of the most useful pages on Django models
Talks about related_name which isn't very well documented IMO