Django advanced query - objects of a Foriegnkey? - django

I have a Django models where I have this :
class Patient(models.Model):
FirstName = models.CharField(max_length=264)
LastName = models.CharField(max_length=264)
Address = models.TextField(blank=True)
Telephone_no = models.CharField(max_length=100,blank=True)
user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,related_name='patients')
class UploadedImages(models.Model):
patient = models.ForeignKey(Patient,on_delete=models.CASCADE,related_name='images')
original = models.ImageField(upload_to = user_directory_path, validators=[validate_file_extension],verbose_name = 'Image')
enhanced = models.ImageField(upload_to=analyses_directory_path, blank=True)
segmented = models.ImageField(upload_to=analyses_directory_path, blank=True)
class Processed(models.Model):
uploaded_image = models.ForeignKey(UploadedImages,on_delete=models.CASCADE,related_name='processed')
pre_analysed = models.ImageField(upload_to=analyses_directory_path, blank=True)
analysedimage = models.ImageField(upload_to=analyses_directory_path, blank=True)
so I want to make queries based on the current user which is user = request.user this is possible in the patient model case as I can make Patient.objects.filter(user=user)
but i can't make it the other 2 models
is there any idea how I can do this?
I didn't add the user FK as I thought I wouldn't need it but now I do?
do i need to add it ? can I make a query without adding the field ?

If you want to query across relationships, Django has explicit syntax for that. For example, to get all the UploadedImage objects for a specific user, use UploadedImage.objects.filter(patient__user=user).

Patient.objects.filter(user=user) returns a queryset, to get patient by user, assuming one Patient has only one user:
patient = Patient.objects.filter(user=user).first()
then you can do:
uploaded_images = patients.images.all()
for image in uploaded_images:
processed = image.processed.all()

Related

Add Queryset to django updateview

I have an updateview in which a manager can go and edit all the fields for the associate. Looks like this:(requirement is to add associate_mgr in the as a dropdown in the updateview)enter image description here
views.py
class ReallocationTeam(LoginRequiredMixin,UpdateView):
model = UserDetails
form_class = ViewEditSample
def get_success_url(self):
return reverse('UserProfile:index')
forms.py
class ViewEditSample(ModelForm):
class Meta:
model = UserDetails
fields = ['associate_name','client','lob','associate_mgr']
The manager should be able to edit the "assciate_mgr" of that associate too.
models.py
associate_name = models.CharField(max_length=200)
associate_nbr = models.CharField(max_length=8, primary_key=True)
associate_email = models.EmailField()
associate_department_id = models.CharField(max_length=50)
associate_mgr = models.CharField(max_length=100,blank=True, null=True)
associate_exec = models.CharField(max_length=100,blank=True, null=True)
associate_org = models.CharField(max_length=100,blank=True,null=True)
title = models.CharField(null=True, blank=True, max_length=100)
date_of_service = models.CharField(null=True,blank=True,max_length=11)
is_manager = models.BooleanField(default=False)
is_exec = models.BooleanField(default=False)
is_team_lead = models.BooleanField(default=False)
but associate_mgr is not a choice field in my db.
I need to add a dropdown that contains associate_mgr in my UpdateView. How do I go about implementing that?
Should I go about writing a query to get all managers and populate them i a dropdow: like this mgr = UserDetails.objects.filter(is_manager=True) But then how do i store the selected in associate_mgr field in db?
You can override your form field in your ModelForm to be a ChoiceField with a list of choices: UserDetails.objects.filter(is_manager=True).values_list('name').
associate_mgr = forms.ChoiceField(choices=
UserDetails.objects.filter(is_manager=True).values_list('associate_name', 'associate_name')
)
Then the choice will automatically be saved (the 'associate_name' field value).
But it would probably be a better idea to use a ForeignKey on your model, rather than a CharField. That would enforce the values to be other UserDetails rather than just a string.

related objects queries django rest framework

I have the following models
class STUser(AbstractBaseUser):
email = models.EmailField(unique=True)
name = models.CharField(max_length=255)
companyname = models.CharField(max_length=200, blank=True, null=True)
...
class VenuePermissions(models.Model):
user = models.ForeignKey(STUser, on_delete=models.CASCADE)
venue = models.ForeignKey(Venue, on_delete=models.CASCADE)
signupvaildatestring = models.CharField(max_length=200, blank=True, null=True)
...
I want to grab all the STUser objects and grab all their permissions.
So what I would like is to grab all the VenuePermissions objects. And grab the user and venue object of each venuePermission
Two ways I can do this. use the VenuePermissions_set attribute on STUser but then how do I grab the venue when its just going to be a pk value?
Or focus on the VenuePermissions objects and grab the user and venue from the pk values but how?
I remember nested queries, and I kinda did one in my browse code.
here is an example:
rooms = Room.objects.filter(venue=OuterRef('pk'), sixtyroundseatingoption= True)
venuelist = venuelist.annotate(sixtyrounds=Exists(rooms))
venuelist = venuelist.filter(Q(sixtyrounds = True) | Q(fullbuyoutsixtyroundseatingoption = True))
I've done the set objects in a serializer before
Example serializer:
class RoomAndImageSerializer(serializers.ModelSerializer):
roomimage_set = RoomImageSerializer(many=True, read_only=True)
class Meta:
model = Room
fields = ('pk','name')
any help with this query would be appreciated!
So this is what I am currently trying, I will post an answer if this works:
class VenueUserList(ListAPIView):
serializer_class = VenueUserListSerializer
queryset = VenuePermissions.objects.select_related('user').select_related('venue').filter(signupvaildatestring=None)
class VenueUserListSerializer(serializers.ModelSerializer):
user = UserSerializer()
venue = VenueSerializer()
class Meta:
model = VenuePermissions
fields = ('user', 'venue', 'isvenueviewer', 'isvenueeventplanner', 'isvenueadministrator')
Here is the answer. However I still need to group venues by user. Working on that.
class VenueUserList(ListAPIView):
serializer_class = VenueUserListSerializer
queryset = VenuePermissions.objects.select_related('user').select_related('venue').filter(signupvaildatestring=None)
class VenueUserListSerializer(serializers.ModelSerializer):
user = UserSerializer()
venue = VenueSerializer()
class Meta:
model = VenuePermissions
fields = ('user', 'venue', 'isvenueviewer', 'isvenueeventplanner', 'isvenueadministrator')

ManyToManyField with extra information

I'm working on a django website and I need to store some information about the user like a isVerified BooleanField and a profile picture in an ImageField as well as ratings which stores ratings a user has given different elements. So I made a model like this:
class UserProfile(AbstractBaseUser):
is_verified = models.BooleanField(default=True)
current_profile = models.ImageField(default=static('img/default_profile.jpg'))
ratings = models.ManyToManyField(Element, on_delete=models.CASCADE)
however I'd like to save some more about these ratings (like a timestamp and the actual value the user rated)
Do I need to make a seperate model just for that or can this be acchieved in a better way?
You need to use intermediary table that can be specified via the through keyword.
For example:
class UserProfile(AbstractBaseUser):
is_verified = models.BooleanField(default=True)
current_profile = models.ImageField(default=static('img/default_profile.jpg'))
ratings = models.ManyToManyField(Element, on_delete=models.CASCADE, through='UserProfileElement')
class UserProfileElement(models.Model):
user = models.ForeignKey(UserProfile, models.CASCADE, verbose_name=_('User'))
element = models.ForeignKey(Element, models.CASCADE, verbose_name=_('Element'))
timestamp = models.DateTimeField(_('Timestamp'), auto_now_add=True)
rating = models.PositiveIntegerField(_('Rating'))
class Meta:
unique_together = ('user', 'element')
Django docs: ManyToManyField.through

Saving to database with one query

I have standard django User model and I have an extended model Member. The Member model is connected to Subscription model.
class Type(models.Model):
limit = models.IntegerField()
name = models.CharField(max_length=50)
default = models.BooleanField()
def __str__(self):
return self.name
class Subscription(models.Model):
started = models.DateField()
type = models.OneToOneField(Type)
def __str__(self):
return self.type.name
class Member(models.Model):
user = models.ForeignKey(to=User)
number = models.CharField(max_length=10)
match_code = models.CharField(max_length=50)
parent = models.ForeignKey("self", null=True, blank=True)
name = models.CharField(max_length=100)
address = models.CharField(max_length=255)
postcode = models.CharField(max_length=10)
city = models.CharField(max_length=100)
country = models.ForeignKey(to=Country, null=True)
language = models.ForeignKey(to=Language)
telephone = models.CharField(max_length=50)
mobile = models.CharField(max_length=50)
main_email = models.EmailField(null=True, blank=True)
active = models.BooleanField()
subscription = models.ForeignKey(Subscription)
def __str__(self):
return self.name
When I create a new user, and when I want to save it to database I do it as follows :
language = Language.objects.get(key="EN")
country = Country.objects.get(key="BE")
# Add to user model
user = User()
user.username = request.POST['email']
user.first_name = request.POST['firstname']
user.last_name = request.POST['lastname']
user.email = request.POST['email']
user.set_password(request.POST['password'])
user.save()
# Add subscription
subscription = Subscription()
subscription.started = datetime.date.today()
subscription.type = Type.objects.filter(default=True)
last_subscription = Subscription.objects.all().order_by('-id')[0]
# Get the last field from user model
last_user = User.objects.all().order_by('-id')[0]
# Add to member model with the last user
member = Member()
member.number = request.POST['member_id']
member.address = request.POST['address']
member.postcode = request.POST['postcode']
member.city = request.POST['city']
member.country = country
member.telephone = request.POST['telephone']
member.mobile = request.POST['mobile']
member.user = last_user
member.language = language
member.active = False
member.subscription = last_subscription
member.save()
But I think that I hit the database to many times. Is there any better solution to do it? Can it be done maybe with one query?
Several things wrong here
subscription.type = Type.objects.filter(default=True)
You are asigning a whole queryset to subscription.type this code would produce an error here. Secondly don't use default as a model field name.
last_subscription = Subscription.objects.all().order_by('-id')[0]
Not quite sure what you are trying to do here. The efficient and error free approach would be to save the subscription object and use it's generated id for your next operation.
user.username = request.POST['email']
user.first_name = request.POST['firstname']
user.last_name = request.POST['lastname']
user.email = request.POST['email']
user.set_password(request.POST['password'])
It's not a good idea to directly pass in post data to models. You should use django forms to validate them first.
last_user = User.objects.all().order_by('-id')[0]
# Add to member model with the last user
This may or may not be the user that you just saved. It's very likely that another instance would have saved another user just after this thread did. It's safer to use user.id (as already mentioned regarding subscription)
Actually, you don't need to get last subscription or last user instance. You can(and should!) just use your subscription and user objects. Because they will have id after the save method will be called."Auto-incrementing primary keys"
Also, it seems that you've never saved the subscription instance.

Get all foreign keys from ORM query in parent table - Django

I have 2 models like such:
class Volunteer(models.Model):
first_name = models.CharField(max_length=50L)
last_name = models.CharField(max_length=50L)
address = models.CharField(max_length=100L)
city = models.CharField(max_length=100L)
state = models.CharField(max_length=2L)
zip = models.CharField(max_length=5L)
skype = models.CharField(max_length=50L)
email = models.CharField(max_length=50L)
age = models.IntegerField(max_length=2L)
phone_mobile = models.CharField(max_length=50L)
phone_home = models.CharField(max_length=50L)
gender = models.CharField(max_length=1)
class Assignment(models.Model):
volunteer = models.ForeignKey(Volunteer)
department = models.ForeignKey(Department)
role = models.ForeignKey(Role)
location = models.CharField(max_length=100L, blank=True, null=True)
I'm trying to get all volunteers as well as their assignment. The following works:
volunteers = Volunteer.objects.all().order_by('last_name')
for volunteer in volunteers:
assignment = Assignment.objects.get(volunteer=volunteer)
volunteer.assignment = assignment
But is there a more Django way of doing it?
In your current model structure, each volunteer can have multiple assignments. So there is no such thing as "the" assignment for a volunteer. Perhaps you meant to use a OneToOneField instead of a ForeignKey, in which case you would automatically get an "assignment" accessor from Volunteer, making your loop redundant.
You can get all assignements of volunteer by using FOO_set, more here in docs:
volunteer.assignement_set.all()