Django: Find all one_to_one relations from queryset - python

I have a queryset of users, which are instances of the model User.
A second model called Patient has a OneToOneField named user:
user = OneToOneField('users.User', on_delete=CASCADE, related_name="patient",
blank=True, null=True)
The goal is to get a queryset of all patients from the queryset of users.
I thought that by using the related_name would be enough, meaning:
queryset_of_users=User.objects.filter(main_group='patients')
queryset_of_patients=queryset_of_users.patient
but it seems this is not it since I get the following error:
AttributeError: 'QuerySet' object has no attribute 'patient'
Any ideas?

Found it,
It works by making a second query:
queryset_of_patients=Patient.objects.filter(user__in=queryset_of_users)

Related

how to get a column value of foreign key in the form of object?

There is 2 models Registration and RegistrationCompletedByUser, I want Registration queryset from RegistrationCompletedByUser with filters(user=request.user, registration__in=some_value, is_completed=True) over RegistrationCompletedByUser. Hence result should be like <QuerySet [<Registration: No name>, <Registration: p2>, <Registration: p-1>]>.
Now what I tried is
Registration.objects.prefetch_related('registrationcompletedbyuser_set') but filters() not working. Another way I tried is model Managers but don't pass parameters for custom filtering.
models.py
class Registration(models.Model):
name=models.CharField(max_length=255)
number=models.SmallIntegerField(null=True, blank=True)
class RegistrationCompletedByUser(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
registration= models.ForeignKey(Registration, on_delete=models.CASCADE)
points = models.SmallIntegerField(default=100)
is_completed = models.BooleanField(default=False)
If I understood this properly, you want to get all Registrations that related to a query instead of a single object.
qs_1 = RegistrationCompletedByUser.objects.filter(user=request.user, is_completed=True).values_list("registration__id", flat=True)
qs_2 = Registration.objects.filter(id__in=qs_1)
As I understood your question is related to django. So actually there is common way to get related query set from another. When you specify ForeignKey to another model actually django automatically creates 'Related Model' + '_set' relation.
I actually didn't get from you question what you are intended to do. In your situation there are many RegistrationCompletedByUser related to one Registration. So what you can do it's to receive all RegistrationCompletedByUser instances from Registration instance by related name for ForeignKey registration of RegistrationCompletedByUser which in your case registration_set. Actually better to specify in RegistrationCompletedByUser model related name as attribute like this:
models.ForeignKey(Registration, on_delete=models.CASCADE,
related_name='registrations')
And after this let's say you have instance of Registration reg1. So to receive queryset of RegistrationCompletedByUser:
reg1.registrations.all()
And you can use filter on it with attributes from Registration model.
And if you want to receive Registration from RegistrationCompletedByUser, again in your case it's just one Registration to many RegistrationCompletedByUser, so let's say we have reg_completed_1, to receive it's only one registration:
reg = reg_completed_1.registration

Set ManyToManyField with particular users from another model's ManyToMany Field

I am building a simple class group app in which I am trying to add particular users from another model's ManyToFieldField to a new model's ManyToFieldField.
class ClassGroup(models.Model):
admins = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='admins')
members = models.ManyToManyField(settings.AITH_USER_MODEL)
title = models.CharField(max_length=9999, default='')
class ClassGroupInvite(models.Model):
class_group = models.ForeignKey(ClassGroup, on_delete=models.CASCADE)
invite_receiver = models.ManyToManyField(class_group.admins.all())
invite_sender = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
As you can see that I am filtering (send request only to class group admins) in ClassGroupInvite with setting ManyToManyField with ClassGroup.admins
But when I try this then it is showing
ManyToManyField(<django.db.models.fields.related_descriptors.ManyToManyDescriptor object at 0x000001CE78793280>) is invalid. First parameter to ManyToManyField must be either a model, a model name, or the string 'self'
I also read the documentation about it, But I didn't find anything about defining it.
then I tried using ClassGroup.admins.all then it showed
AttributeError: 'ManyToManyDescriptor' object has no attribute 'all'
I have tried many times but it is still not working, Any help would be much Appreciated. Thank You in Advance.

access django fk related objects in view as template

I have a models as
class Doctor(models.Model):
user = models.OneToOneField(
User,
on_delete=models.CASCADE,
primary_key=True,
related_name="user")
# other fields...
In my template, I easily can access the doctor object as request.user.doctor but using it in my views it causes the 'User' object has no attribute 'doctor' Error. so is it possible to access it as templates in my views too.
The related_name=… parameter [Django-doc] is the name of the relation in reverse, so to access the Doctor object from a User, since you have set this to user, you thus access the Doctor object with request.user.user, but that is misleading.
You thus better rename the relation to:
class Doctor(models.Model):
user = models.OneToOneField(
User,
on_delete=models.CASCADE,
primary_key=True,
related_name='doctor'
)
# other fields …
Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.

How to retrieve entries from a many-to-many relationship

I have a application, that is used to manage assistant jobs. Therefore, the model is composed of 3 models: Person, Course, Application (typical many-to-many relation).
My models.py looks as follow:
class Person(AbstractUser):
...
class Course(models.Model):
year = models.charField(max_length=9)
term = ...
class Applications(models.Model):
applicant = models.ForeignKey(Person, on_delete=models.CASCADE, related_name="applicant")
course = models.ForeignKey(Course, on_delete=models.CASCADE)
status = models.CharField(max_length=255, default='Pending')
In the context of a form, I need to retrieve all the courses a person has been hired in order to populate a dropdown list.
It is easy to get all the applications of the currently logged in user having the status 'Hired':
Applications.objects.filter(applicant=user, status="Hired")
but I can't get a a queryset of all the related courses:
Applications.objects.filter(applicant=user, status="Hired").course_set
returns me an:
AttributeError: 'QuerySet' object has no attribute 'course_set'
As per Django documentation, this attribute should exist.
What am I doing wrong?
The reverse accessor course_set is available on an instance of Applications model, not on the queryset (which Applications.objects.filter returns).
For example, if you have an Applications instance named application, you can do:
application.course_set.all()
to get all the instances of Course that are related to application.
If you want to get the related Course instances from filtered Applicaitons:
Applications.objects.filter(
applicant=user, status="Hired"
).values_list(
'course', flat=True
).distinct()
This will return the primary keys of related Course instances.
Just use _set to access it.
Try with the docs first, to get the idea.
https://docs.djangoproject.com/en/3.0/ref/models/relations/

How to prefetch SocialAccount with django-allauth?

Django-allauth package creates a model SocialAccount, with a ForeignKey to User. I can't manage to prefetch this information in my queryset.
My model:
class Placerating(models.Model):
author = models.ForeignKey('auth.User', null=True, on_delete=models.SET_NULL)
Django-allauth model:
class SocialAccount(models.Model):
user = models.ForeignKey(allauth.app_settings.USER_MODEL, on_delete=models.CASCADE)
When I try to prefetch this data:
rating = Placerating.objects.all().prefetch_related('author__socialaccount')
I get the following error message:
AttributeError: Cannot find 'socialaccount' on User object, 'author__socialaccount' is an invalid parameter to prefetch_related()
Any clue ? Thanks!
I got my answer.
SocialAccount is a reverse Foreign key, so "_set" must be added at the end:
rating = Placerating.objects.all().prefetch_related('author__socialaccount_set')

Categories