Django getting related objects from M2M - python

For example, I have three Models in django:
class Car(models.Models):
range = models.DecimalField()
speed = models.DecimalField()
def __str__(self):
return self.speed
class Group_of_Cars(models.Models):
name = models.CharField()
starting_city = models. CharField()
car = models.ManyToManyField(Car)
def __str__(self):
return self.name
class Arrival_time(models.Models):
Location_of_ArrivalPoint = models.CharField()
Last_known_location_of_CarGroup = models.CharField()
Group_of_Cars = models.ForeignKey(Group_of_Cars)
def function(self):
"Get speed of the "Car" in "Group_of_Cars"
def __str__(self):
return self. Location_of_ArrivalPoint
This is an example of what I want to do, not my actual models. The idea is for the user to input a series of values for the type of "Cars" such as speed and range. I'd like "Cars" to be selected when defining parameters for "a Group_of_Cars". What I'm not sure how to do is how to get the speed of the car, for the Group_of_Cars for which I need to calculate an arrival time (Group_of_Cars consists of one type of car and I'd like 'Arrival_time' to be its own table).
Thank you for any input.

models.py
from django.db import models
# Create your models here.
class Car(models.Model):
range = models.DecimalField(decimal_places=2,max_digits=5)
speed = models.DecimalField(decimal_places=2,max_digits=5)
def __str__(self):
return str(self.speed)
class Group_of_Cars(models.Model):
name = models.CharField(max_length=50)
starting_city = models. CharField(max_length=50)
car = models.ManyToManyField(Car)
def __str__(self):
return self.name
class Arrival_time(models.Model):
Location_of_ArrivalPoint = models.CharField(max_length=50)
Last_known_location_of_CarGroup = models.CharField(max_length=50)
Group_of_Cars = models.ForeignKey(Group_of_Cars, on_delete=models.CASCADE)
def function(self):
# "Get speed of the "Car" in "Group_of_Cars"
return self.Group_of_Cars.car.all()
def __str__(self):
return self. Location_of_ArrivalPoint
In views.py
from django.shortcuts import render
from core.models import Arrival_time
from django.http import HttpResponse
# Create your views here.
def home(request):
arp = Arrival_time.objects.get(Location_of_ArrivalPoint='Newyork')
for i in arp.function():
print(i)
return HttpResponse()

Related

Python 3 Django Rest Framework - how to add a custom manager to this M-1-M model structure?

I have these models:
Organisation
Student
Course
Enrollment
A Student belongs to an Organisation
A Student can enrol on 1 or more courses
So an Enrollment record basically consists of a given Course and a given Student
from django.db import models
from model_utils.models import TimeStampedModel
class Organisation(TimeStampedModel):
objects = models.Manager()
name = models.CharField(max_length=50)
def __str__(self):
return self.name
class Student(TimeStampedModel):
objects = models.Manager()
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
email = models.EmailField(unique=True)
organisation = models.ForeignKey(to=Organisation, on_delete=models.SET_NULL, default=None, null=True)
def __str__(self):
return self.email
class Course(TimeStampedModel):
objects = models.Manager()
language = models.CharField(max_length=30)
level = models.CharField(max_length=2)
def __str__(self):
return self.language + ' ' + self.level
class Meta:
unique_together = ("language", "level")
class EnrollmentManager(models.Manager):
def org_students_enrolled(self, organisation):
return self.filter(student__organisation__name=organisation).all()
class Enrollment(TimeStampedModel):
objects = EnrollmentManager()
course = models.ForeignKey(to=Course, on_delete=models.CASCADE, default=None, null=False, related_name='enrollments')
student = models.ForeignKey(to=Student, on_delete=models.CASCADE, default=None, null=False, related_name='enrollments')
enrolled = models.DateTimeField()
last_booking = models.DateTimeField()
credits_total = models.SmallIntegerField(default=10)
credits_balance = models.DecimalField(max_digits=5, decimal_places=2)
Notice the custom EnrollmentManager that allows me to find all students who are enrolled from a given organisation.
How can I add a custom Manager to retrieve all the courses from a given organisation whose students are enrolled?
What I have tried
I thought to create a CourseManager and somehow query/filter from that side of the relationship:
class CourseManager(models.Manager):
def org_courses_enrolled(self, organisation):
return self.filter(enrollment__student__organisation__name=organisation).all()
This works, but it gives me the same 100 enrollment records :(
What I am trying to get is:
based on a given organisation
find all students who are enrolled
and then (DISTINCT?) to get the list of enrolled courses for that org
This is the view:
class OrganisationCoursesView(mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
serializer_class = CourseSerializer
queryset = Course.objects.get_courses(1)
and the url:
# The below should allow: /api/v1/organisations/1/courses/
router.register('api/v1/organisations/(?P<organisation_pk>\d+)/courses', OrganisationCoursesView, 'organisation courses')
UPDATE 1
Based on the answer from h1dd3n I tried this next:
class CourseManager(models.Manager):
def get_queryset(self):
return super(CourseManager, self).get_queryset()
def get_courses(self, organisation):
return self.get_queryset().filter(student__organisation_id=organisation)
but that throws an error (as I expected it would):
FieldError: Cannot resolve keyword 'student' into field. Choices are:
courses, created, id, language, level, modified, progress
UPDATE 2 - getting closer!
Ok with help from #AKX's comments:
class CourseManager(models.Manager):
def get_queryset(self):
return super(CourseManager, self).get_queryset()
def get_courses(self, organisation):
return self.get_queryset().filter(courses__student__organisation_id=organisation)
now DOES return courses, but it returns a copy for each enrolled student. So now I need to group them so each record only appears one time...
First you need to change self.filter to self.get_queryset().filter() or make a seperate method in the manager.
def get_queryset(self):
return super(CourseManager, self).get_queryset()
In manager create a function
def get_courses(self,organisation):
return self.get_queryset.filter(student__oraganisation=organisation)
This should return the students and you don't need to call .all() - the filtered qs either way returns you all the objects that it finds.
EDIT
Try this:
class CourseManager(models.Manager):
def get_queryset(self):
return super(CourseManager, self).get_queryset()
def get_courses(self, organisation):
return self.get_queryset().filter( \
enrollments__student__organisation_id=organisation).distinct()
UPDATE 2
You can try and play around with from django.db.models import Q https://docs.djangoproject.com/en/3.0/topics/db/queries/
or with annotate based on this answer Get distinct values of Queryset by field where you filter out each student so it would appear once.

unable to auto-fill data during update in dependent dropdowns

So I have been following this tutorial related to dependent dropdowns which can be found here and I seem to have gotten stuck at the last part the else if statement and am unable to implement the same in my project
postlog models.py:
from django.db import models
# Create your models here.
class FlightNum(models.Model):
fl_no =models.CharField(max_length=5,primary_key=True)
def __str__(self):
return self.fl_no
class Destination(models.Model):
fl_no= models.OneToOneField(FlightNum,on_delete=models.CASCADE,related_name='fl1')
dest= models.CharField(max_length=15,blank=True)
def __str__(self):
return self.dest
class Parking(models.Model):
fl_no= models.OneToOneField(FlightNum,on_delete=models.CASCADE,related_name='fl2')
park_bay= models.CharField(max_length=3)
def __str__(self):
return self.park_bay
class DepartureTime(models.Model):
fl_no= models.OneToOneField(FlightNum,on_delete=models.CASCADE,related_name='fl3')
dep_time= models.CharField(max_length=9)
def __str__(self):
return self.dep_time
class Flight(models.Model):
fl_no= models.OneToOneField(FlightNum,on_delete=models.CASCADE, primary_key=True, related_name='fl4')
park_bay= models.ForeignKey(Parking, on_delete=models.CASCADE)
dest= models.ForeignKey(Destination, on_delete=models.CASCADE)
dep_time= models.ForeignKey(DepartureTime, on_delete=models.CASCADE)
inbound= models.CharField(max_length=15,blank=True)
airline= models.CharField(max_length=15)
arr_time= models.TimeField()
status models.py:
from django.db import models
from postlog.models import FlightNum,Destination,Parking,DepartureTime
# Create your models here.
class FlightStatus(models.Model):
CLEANING_CHOICES = (
('Yes', 'Yes'),
('No','No'),
)
fl_no= models.OneToOneField(FlightNum,on_delete=models.CASCADE,related_name='fli_no',primary_key=True)
park_bay= models.ForeignKey(Parking,on_delete=models.CASCADE,related_name='parki_bay')
catering= models.CharField(max_length=9)
fuel= models.IntegerField()
pas_cnt= models.IntegerField()
dest= models.ForeignKey(Destination,on_delete=models.CASCADE,related_name='desti',null=True)
dep_time=models.ForeignKey(DepartureTime,on_delete=models.CASCADE,related_name='dept_time')
Cleaning = models.CharField( max_length=3, choices=CLEANING_CHOICES)
status forms.py:
from django.forms import ModelForm
from status.models import FlightStatus
from postlog.models import FlightNum,Destination,Parking,DepartureTime
class StatusForm(ModelForm):
class Meta:
model = FlightStatus
fields = ('fl_no', 'park_bay', 'catering', 'fuel', 'pas_cnt', 'dest','dep_time', 'Cleaning')
labels = {
'fl_no': ('Flight Number'),
'park_bay': ('Parking Bay'),
'catering': ('Catering'),
'fuel': ('Fuel'),
'pas_cnt': ('Passenger Count'),
'dest': ('Destination'),
'dep_time': ('Departure Time'),
'Cleaning': ('Cleaning'),
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['park_bay'].queryset = Parking.objects.none()
self.fields['dest'].queryset = Destination.objects.none()
self.fields['dep_time'].queryset = DepartureTime.objects.none()
if 'fl_no' in self.data:
try:
flightid = int(self.data.get('fl_no'))
self.fields['park_bay'].queryset = Parking.objects.filter(fl_no=flightid).order_by('park_bay')
self.fields['dest'].queryset = Destination.objects.filter(fl_no=flightid).order_by('dest')
self.fields['dep_time'].queryset = DepartureTime.objects.filter(fl_no=flightid).order_by('dep_time')
except (ValueError, TypeError):
pass
elif self.instance.pk:
self.fields['park_bay'].querysets = self.instance.fl_no.park_bay_set.order_by('park_bay')
self.fields['dest'].querysets = self.instance.fl_no.dest_set.order_by('dest')
self.fields['dep_time'].querysets = self.instance.fl_no.park_bay_set.order_by('dep_time')
so if anyone could help me fix this part:
elif self.instance.pk:
self.fields['park_bay'].querysets = self.instance.fl_no.park_bay_set.order_by('park_bay')
self.fields['dest'].querysets = self.instance.fl_no.dest_set.order_by('dest')
self.fields['dep_time'].querysets = self.instance.fl_no.dep_time_set.order_by('dep_time')
I would really appreciate it
The error says there's no park_bay_set attribute for FlightNum, because you specified related_name='fl4' for your fl_no field inside your Flight model. Also, up to this point you are in a Flight instance, so you need one more relation to reach the park bays. Your status forms.py should be like this:
elif self.instance.pk:
self.fields['park_bay'].querysets = self.instance.fl_no.fl4.park_bay.all().order_by('park_bay')
self.fields['dest'].querysets = self.instance.fl_no.dest_set.order_by('dest')
self.fields['dep_time'].querysets = self.instance.fl_no.fl4.park_bay.all().order_by('dep_time')
Here's a diagram of how your models are currently related, not sure if this is exactly what you want, you might want to take a look and see if a normalization is needed.

Django model relationship - Fitness App

Currently my models are:
class Workout(models.Model):
date = models.DateField()
routine = models.ForeignKey('Routine')
def __str__(self):
return '%s' % self.date
class Routine(models.Model):
name = models.CharField(max_length=255)
exercises = models.ManyToManyField('Exercise')
def __str__(self):
return self.name
class Exercise(models.Model):
name = models.CharField(max_length=255)
def __str__(self):
return self.name
I want the user to be able to create a new entry specified by a date(Workout). They can also create routines(Routine), associated with the date and filled with different exercises(Exercise) which they can also create.
Here is the part I can't figure out.
I want the user, when adding a new exercise, to be able to choose whether it is a strength exercise or cardio exercise. Strength exercises will have fields like: #of sets, reps, and weight. Where as carido will have fields like length and speed.
I am unclear on how to relate the two types of exercises to the Exercise class.
The most common way of doing this, is to create a generic relationship, such as:
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
class Exercise(models.Model):
name = models.CharField(max_length=255)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
info = GenericForeignKey('content_type', 'object_id')
def __str__(self):
return self.name
class StrengthExercise(models.Model):
sets, reps, weight = (...)
class CardioExercise(models.Model):
length, speed = (...)
Example use:
>>> from app_name.models import Exercise, CardioExercise
>>> exercise_info = CardioExercise.objects.create(length=600, speed=50)
>>> exercise = Exercise(name="cardio_exercise_1", info=exercise_info)
>>> exercise.save()
>>> exercise.info.length
600
>>> exercise.info.__class__.__name__
'CardioExercise'
OBS: Make sure you have 'django.contrib.contenttypes' in your INSTALLED_APPS (enabled by default).

How to filter records of ManyToManyField?

I have following Django models:
class TopicLabel(models.Model):
name = models.CharField(max_length=256)
order = models.IntegerField(null=True, blank=True)
topics = models.ManyToManyField(Topic, through='TopicLabelConnection')
class Topic(models.Model):
title = models.CharField(max_length=140)
visible = models.NullBooleanField(null=True, blank=True, default=False)
def __unicode__(self):
return self.title
class Meta:
verbose_name = _('topic')
verbose_name_plural = _('topics')
class TopicLabelConnection(models.Model):
topicId = models.ForeignKey(Topic, related_name='connection_topic')
labelId = models.ForeignKey(TopicLabel, related_name='connection_label')
def __unicode__(self):
return self.labelId.name + ' / ' + self.topicId.title
I want to create a method of TopicLabel, which will return to me all topics in the TopicLabel.topic collection, which have Topic.visible = True.
I want to program a Django equivalent of the following query:
SELECT *
FROM OPINIONS_TOPICLABELCONNECTION, OPINIONS_TOPIC
WHERE (OPINIONS_TOPICLABELCONNECTION.topicId_id = OPINIONS_TOPIC.id) AND
(OPINIONS_TOPICLABELCONNECTION.labelId_id = X) AND
(OPINIONS_TOPIC.visible = 1)
where X is the primary key of the topic label.
I tried following method definitions and they all failed:
1)
class TopicLabel(models.Model):
[...]
def getVisibleTopics():
return topics.filter(connection_topic__visible=True)
2)
class TopicLabel(models.Model):
[...]
def getVisibleTopics():
return topics.filter(visible=True)
3)
class TopicLabel(models.Model):
[...]
def getVisibleTopics():
return Topic.objects.filter(connection_label__visible=True).filter(connection_label__id=self.id)
4)
class TopicLabel(models.Model):
[...]
def getVisibleTopics():
return Topic.objects.filter(connection_label__visible=True).filter(connection_label__id=self.id)
5)
class TopicLabel(models.Model):
[...]
def getVisibleTopics():
return topics.filter(connection_topicId__visible=True)
What is the correct code?
First, you need to have self as the first parameter on a method. Then filter the topics. try this:
class TopicLabel(models.Model):
[...]
def getVisibleTopics(self):
return self.topics.filter(visible=True)
Also, is there a reason you are creating a custom through table? It doesn't look like you are adding any extra data to it.

How do I filter values in a Django form using ModelForm?

I am trying to use the ModelForm to add my data. It is working well, except that the ForeignKey dropdown list is showing all values and I only want it to display the values that a pertinent for the logged in user.
Here is my model for ExcludedDate, the record I want to add:
class ExcludedDate(models.Model):
date = models.DateTimeField()
reason = models.CharField(max_length=50)
user = models.ForeignKey(User)
category = models.ForeignKey(Category)
recurring = models.ForeignKey(RecurringExclusion)
def __unicode__(self):
return self.reason
Here is the model for the category, which is the table containing the relationship that I'd like to limit by user:
class Category(models.Model):
name = models.CharField(max_length=50)
user = models.ForeignKey(User, unique=False)
def __unicode__(self):
return self.name
And finally, the form code:
class ExcludedDateForm(ModelForm):
class Meta:
model = models.ExcludedDate
exclude = ('user', 'recurring',)
How do I get the form to display only the subset of categories where category.user equals the logged in user?
You can customize your form in init
class ExcludedDateForm(ModelForm):
class Meta:
model = models.ExcludedDate
exclude = ('user', 'recurring',)
def __init__(self, user=None, **kwargs):
super(ExcludedDateForm, self).__init__(**kwargs)
if user:
self.fields['category'].queryset = models.Category.objects.filter(user=user)
And in views, when constructing your form, besides the standard form params, you'll specify also the current user:
form = ExcludedDateForm(user=request.user)
Here example:
models.py
class someData(models.Model):
name = models.CharField(max_length=100,verbose_name="some value")
class testKey(models.Model):
name = models.CharField(max_length=100,verbose_name="some value")
tst = models.ForeignKey(someData)
class testForm(forms.ModelForm):
class Meta:
model = testKey
views.py
...
....
....
mform = testForm()
mform.fields["tst"] = models.forms.ModelMultipleChoiceField(queryset=someData.objects.filter(name__icontains="1"))
...
...
Or u can try something like this:
class testForm(forms.ModelForm):
class Meta:
model = testKey
def __init__(self,*args,**kwargs):
super (testForm,self ).__init__(*args,**kwargs)
self.fields['tst'].queryset = someData.objects.filter(name__icontains="1")
I know this is old; but its one of the first Google search results so I thought I would add how I found to do it.
class CustomModelFilter(forms.ModelChoiceField):
def label_from_instance(self, obj):
return "%s %s" % (obj.column1, obj.column2)
class CustomForm(ModelForm):
model_to_filter = CustomModelFilter(queryset=CustomModel.objects.filter(active=1))
class Meta:
model = CustomModel
fields = ['model_to_filter', 'field1', 'field2']
Where 'model_to_filter' is a ForiegnKey of the "CustomModel" model
Why I like this method:
in the "CustomModelFilter" you can also change the default way that the Model object is displayed in the ChoiceField that is created, as I've done above.
is the best answer:
BookDemoForm.base_fields['location'] = forms.ModelChoiceField(widget=forms.Select(attrs={'class': 'form-control select2'}),queryset=Location.objects.filter(location_for__fuel=True))

Categories