Single object from queryset - python

I need help with a little issue I am having with my project. I have a model where I have a foreign key with many entries and I would like to take just one item of these entries.
In fact, my problem is something like that:
I need to obtain the single mission id, in order to have other functions.
The code is working fine, my problem is that I don't know the righe order to obtain the mission item from this queryset:
models.py
from django.db import models
from flight.models import LogEntry
from user.models import Instructor, Student
# Create your models here.
TRAINING_COURSE_CHOICES = (
('PPL', 'PPL'),
('CPL', 'CPL'),
('IR', 'IR'),
('ATPL', 'ATPL'),
)
class TrainingCourse(models.Model):
name = models.CharField(max_length=20, choices=TRAINING_COURSE_CHOICES)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
class StudentTrainingCourse(models.Model):
training_course = models.ForeignKey(
TrainingCourse, on_delete=models.PROTECT)
student = models.ForeignKey(Student, on_delete=models.PROTECT)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.student.first_name + ' ' + self.student.last_name + ' ' + self.training_course.name
class Mission(models.Model):
name = models.CharField(max_length=200)
training_course = models.ForeignKey(
TrainingCourse, on_delete=models.CASCADE)
note = models.TextField(null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.training_course.name + ' ' + self.name
class StudentMission(models.Model):
mission = models.ForeignKey(Mission, on_delete=models.PROTECT)
student_training_course = models.ForeignKey(
StudentTrainingCourse, on_delete=models.PROTECT)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['mission__name']
def __str__(self):
return self.mission.name
class LearningObjective(models.Model):
name = models.CharField(max_length=300)
mission = models.ForeignKey(Mission, on_delete=models.DO_NOTHING)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
GRADING_VALUE = (
('1', '1'),
('2', '2'),
('3', '3'),
('4', '4'),
('5', '5'),
)
class MissionEntry(models.Model):
student = models.ForeignKey(
Student, on_delete=models.DO_NOTHING, blank=True, null=True)
mission = models.ForeignKey(
Mission, on_delete=models.DO_NOTHING, null=True, blank=True)
log_entry = models.ForeignKey(
LogEntry, on_delete=models.DO_NOTHING, blank=True, null=True)
learning_objective = models.ForeignKey(
LearningObjective, on_delete=models.DO_NOTHING, blank=True, null=True)
grade = models.CharField(
max_length=10, choices=GRADING_VALUE, blank=True, null=True)
note = models.TextField(blank=True, null=True)
debriefing = models.TextField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return str(self.mission) + ' ' + str(self.log_entry)
class Meta:
verbose_name_plural = 'Mission Entries'
class MissionEntryStatus(models.Model):
student_mission = models.ForeignKey(
StudentMission, on_delete=models.PROTECT, null=True, blank=True)
mission = models.ForeignKey(
Mission, on_delete=models.PROTECT, null=True, blank=True)
student = models.ForeignKey(Student, on_delete=models.PROTECT)
is_completed = models.BooleanField(default=False)
is_failed = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
views.py
def student_mission_list(request, pk):
student = Student.objects.get(id=pk)
student_training_course = StudentTrainingCourse.objects.get(
student_id=student)
missions = StudentMission.objects.filter(
student_training_course_id=student_training_course)
context = {
'student': student,
'missions': missions,
}
return render(request, 'mission/student_mission_list.html', context)
In fact, in student_mission_list I need to retrieve the mission_id!!! The problem is that, if I try to retrieve that from StudentTraining or MissionEntryStatus model, I have multiple missions; but i'd like to have just the corresponding mission_id, but I am not able to find a way.
Is there any suggestion? Probably I have to modify the database, but I don't know where.
Thank you very much to all of you for your help

The way you have it set up now, a StudentTrainingCourse can have only ONE student but, because a ForeignKey is a ManyToOne relationship, MANY students can have that same StudentTrainingCourse. What I think you want is for a Student to have ONE StudentTrainingCourse, but a StudentTrainingCourse to have MANY Students. My guess is what you need is:
class Student(models.Model):
...
training_course = models.ForeignKey(StudentTrainingCourse, on_delete=models.CASCADE)
...
Then you can get the particular mission id like:
def student_mission_list(request, pk):
student = Student.objects.get(id=pk)
training_course = student.training_course
mission = Mission.objects.get(training_course=training_course)
# If you just want the mission ID:
mission_id = mission.pk
missions = StudentMission.objects.filter(
student_training_course_id=student_training_course)
context = {
'student': student,
'missions': missions,
}
return render(request, 'mission/student_mission_list.html', context)

Related

Cannot assign "'Sample Category'": "Product.category" must be a "Category" instance

While creating new products I'm getting such kind of error. Can someone help me?
class Product(models.Model):
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
name_geo = models.CharField(max_length=200, null=True, blank=True)
image = models.ImageField(null=True, blank=True, default='/placeholder.png')
brand = models.CharField(max_length=200, null=True, blank=True)
category = models.ForeignKey(Category, null=False, default=0, on_delete=models.CASCADE)
price = models.DecimalField(max_digits=7, decimal_places=2, null=True, blank=True)
countInStock = models.IntegerField(null=True, blank=True, default=0)
createdAt = models.DateTimeField(auto_now_add=True)
_id = models.AutoField(primary_key=True, editable=False)
def __str__(self):
return self.name_geo
class Category(models.Model):
_id = models.AutoField(primary_key=True, editable=False)
name = models.CharField(max_length=200, null=True, blank=True)
createdAt = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.name
#api_view(['POST'])
def createProduct(request):
user = request.user
product = Product.objects.create(
user=user,
name_geo="Sample Name",
category="Sample Category",
price=0,
brand='Sample Brand',
countInStock=0,
)
serializer = ProductSerializer(product, many=False)
return Response(serializer.data)
Without separating category class in models.py everything works fine. I mean If i didn't use ForeignKey in Products class for category
It just has to be a Category Instance/Object
product = Product.objects.create(
user=user,
name_geo="Sample Name",
category=Category.objects.get_or_create(name="Sample Category"),
price=0,
brand='Sample Brand',
countInStock=0,
)
Notes:
You could just do a .get() or a .filter().first() if you don't want to create
If you use a form, you can get away with just the Category's PK/_id in the POST
this type of thing: f = form(request.POST) f.is_valid() f.save()
At the end that field will hold the PK/_id/Row# of the Category Obj

Django _set.all() filter in QuerySet

I have database and I want to extract specific data of specific user from queryset. Now i have this
VIEW
def index(request):
customerByName = Customer.objects.get(name='pablo')
shopListById = ShopList.objects.get(transaction_id=1)
shpoListSpecific = customerByName.shoplist_set.all()
specificProducts = shopListById.shoplistproduct_set.all()
context = {'customerByName':customerByName, 'shpoListSpecific':shpoListSpecific, 'shopListById':shopListById,
'specificProducts': specificProducts}
return render(request, 'QuickShopperApp/home.html', context)
MODELS
class Customer(models.Model):
user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE)
name = models.CharField(max_length=200, null=True, blank=True)
email = models.CharField(max_length=200, null=True, blank=True)
device = models.CharField(max_length=200, null=True, blank=True)
def __str__(self):
if self.name:
name = self.name
else:
name = self.device
return str(name)
class Product(models.Model):
name = models.CharField(max_length=200, null=True)
def __str__(self):
return self.name
class ShopList(models.Model): # cart
customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, null=True, blank=True)
#product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True)
date_ordered = models.DateTimeField(auto_now_add=True)
complete = models.BooleanField(default=False)
transaction_id = models.CharField(max_length=100, null=True)
def __str__(self):
return str(self.id)
class ShopListProduct(models.Model): # each ShopList will have multiple ShopListProduct
product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True)
shopList = models.ForeignKey(ShopList, on_delete=models.SET_NULL, null=True) #shoplistitem.shoplist
quantity = models.IntegerField(default=0, null=True, blank=True)
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return str(self.product)
template.html
<h3>specificProducts: {{specificProducts}}</h3>
On my side i see items of specific customer.
specificProducts: <QuerySet [<ShopListProduct: Apple>, <ShopListProduct: Cucumber>, <ShopListProduct: Cucumber>]>
How can i get only Apple, Cucumber, Cucumber?
try this
'specificProducts': specificProducts.values_list('product__name')
or
'specificProducts': list(specificProducts.values_list('product__name', flat=True))
https://docs.djangoproject.com/en/3.1/ref/models/querysets/#values-list

Django: get the max count of a foreign key based on other foreign key

I have this Model:
class Complaint(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, default=1)
date_created = models.DateTimeField(auto_now_add=True, null=True, blank=True)
name = models.CharField(max_length=255, unique=True)
definition = models.TextField(blank=False, default="")
is_violent = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
def __str__(self):
return self.name
class Meta:
ordering = ['name']
def get_absolute_url(self):
return reverse('complaint-details', kwargs={'pk': self.pk})
class Service(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, default=1)
date_created = models.DateTimeField(auto_now_add=True, null=True, blank=True)
name = models.CharField(max_length=255, unique=True)
definition = models.TextField(blank=True, default="")
is_active = models.BooleanField(default=True)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('service-details', kwargs={'pk': self.pk})
class Location(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, default=1)
date_created = models.DateTimeField(auto_now_add=True, null=True, blank=True)
location_name = models.CharField(max_length=255, unique=True)
loc_lat = models.DecimalField(max_digits=9, decimal_places=6)
loc_long = models.DecimalField(max_digits=9, decimal_places=6)
pop = models.PositiveIntegerField(default=500)
is_AOR = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
def __str__(self):
return self.location_name
class Blotter(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, default=1)
date_created = models.DateTimeField(auto_now_add=True, null=True, blank=True)#default=timezone.now().date()
date = models.DateField(blank=True)
time = models.TimeField(blank=True)
entry_number = models.CharField(max_length=255, unique=True,validators=[RegexValidator(r'^\d{1,255}$')])
complaints = models.ForeignKey(Complaint, on_delete=models.CASCADE, null=True, blank=True)
service = models.ForeignKey(Service, on_delete=models.CASCADE, null=True, blank=True)
information = models.TextField(blank=False, default="")
location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True, blank=True)
is_active = models.BooleanField(default=True)
class Meta:
ordering = ("date_created",)
def __str__(self):
return (self.entry_number)
def get_absolute_url(self):
return reverse('details-blotter', kwargs={'pk': self.pk})
And I have this serializer:
class APILocationListSerializer(serializers.Serializer):
address = serializers.CharField()
latitude = serializers.DecimalField(max_digits=9, decimal_places=5)
longitude = serializers.DecimalField(max_digits=9, decimal_places=5)
population= serializers.IntegerField()
crime_count=serializers.IntegerField()
crime_rate=serializers.DecimalField(max_digits=4, decimal_places=3)
is_aor = serializers.BooleanField()
class Meta:
model = Blotter
fields= [
'address',
'latitude',
'longitude',
'population',
'crime_count',
'crime_rate'
'is_aor',
]
def to_representation(self, value):
context = {
value['address']:
{
'coordinates':[value['latitude'],value['longitude']],
'Population': value['population'],
'Crime-Count': value['crime_count'],
'Crime-Rate': value['crime_rate'],
'Area-Of-Responsibility': value['is_aor'],
}
}
return context
And ListApiView:
class APILocationList(generics.ListAPIView):
serializer_class = APILocationListSerializer
def get_queryset(self):
q=Blotter.objects.values('location__location_name').annotate(
address=F('location__location_name'),
latitude=F('location__loc_lat'),
longitude=F('location__loc_long'),
population=F('location__pop'),
crime_count=Count('complaints', filter=Q(complaints__is_active=True) and Q(complaints__isnull=False)),
crime_rate=(Cast(F('crime_count'), FloatField())/Cast(F('population'), FloatField()))*100000,
is_aor=F('location__is_AOR')
)
q1 = q.filter(location__is_AOR=True).order_by('address')
query_search = self.request.GET.get("q")
if query_search:
q1 = q.filter(Q(location__is_AOR=True) and Q(location__location_name__icontains=query_search)).order_by('address')
return q1
I'm new to django and DRF. I want to achieve a result like this in my API Not Achieved
but this is the result that i've achieved so far Achieved
As you can see in the picture, I want to count the crime trend (highest count of a crime and the crime itself) in every location.
My questions are:
Is this even achievable/possible to get these results using just one query?
If yes, how?
If no, is there any other way to achieve these kind of results?
Thank you in advance!

DRF get 2 row with bigger count than other ones

i have some row in table i want to get only 2 record that have bigger count value that others . for now according the my picture count 50 and 80 shoud be return.
i should have a list of product_ids (only 2 record)
that have count more that others.. so i should try values_list
i know this is wrong how can i fix it?
prod_ids = ProductViewCount.objects.all().aggregate(Max('count')).values_list('product', flat=True)
this is my full code :
class ViewTopLiked(APIView):
def get(self, request):
prod_ids = ProductViewCount.objects.all().aggregate(Max('count')).values_list('product', flat=True)
obj = Product.objects.filter(product_id__in=prod_ids).order_by('-created_date')
serializer = ProductSerializer(instance=obj, many=True, context={'request': request})
return Response(serializer.data)
productViewcount model:
model:
class ProductViewCount(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE, to_field='product_id',
related_name='count')
count = models.IntegerField(null=True, blank=True, default=0)
product model:
class Product(models.Model):
PRO = 'P'
INTER = 'I'
BEGINER = 'B'
ALL = 'A'
TYPE_CHOICE = ((PRO, 'P'), (INTER, 'I'), (BEGINER, 'B'), (ALL, 'A'))
product_id = models.AutoField(primary_key=True)
author = models.ForeignKey(User, on_delete=models.CASCADE, db_index=True)
title = models.CharField(max_length=200)
video_length = models.IntegerField(null=True, blank=True)
mini_description = models.CharField(max_length=1000, null=True, blank=True)
full_description = models.TextField(null=True, blank=True)
price = models.IntegerField(null=True, blank=True)
free = models.BooleanField(default=False)
video_level = models.CharField(max_length=20, choices=TYPE_CHOICE, default=ALL)
created_date = models.DateTimeField(auto_now_add=True)
updated_date = models.DateTimeField(auto_now=True)
publish = models.BooleanField(default=False)
draft = models.BooleanField(default=False)
slug = models.SlugField(allow_unicode=True, null=True, blank=True)
image = models.FileField(upload_to=upload_to_custom_p, null=True, blank=True)
lecture = models.IntegerField(null=True, blank=True)
def __str__(self):
return self.title
#property
def owner(self):
return self.author
For ordering you can use count related_name in product queryset, to get only first two elements use slicing [:2]:
obj = Product.objects.order_by('-count__count')[:2]
serializer = ProductSerializer(instance=obj, many=True, context={'request': request})
return Response(serializer.data)

Django: Reference between models

Help!
I have the following 2 models:
class Account(models.Model):
username = models.OneToOneField(User, primary_key=True, unique=True)
receiveaddress = models.CharField(max_length=40, blank=True, null=True, unique=True)
balance = models.DecimalField(max_digits=16, decimal_places=8, default=0)
def __str__(self):
return str(self.username)
class Deposits(models.Model):
receiveaddress = models.CharField(max_length=40, blank=True, null=True, unique=True)
amount = models.DecimalField(max_digits=16, decimal_places=8, default=0)
user = ?????????????????
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
confirmed = models.BooleanField(default=False)
accounted = models.BooleanField(default=False)
def __str__(self):
return str(self.receiveaddress)
Example:
Visualization
My problem:
I want "Deposits.user" to automatically reference the user to which this 'receiveaddress' belongs. In the example, that's TIM. I've wasted 6 hours trying to figure it out, what am I doing wrong?
Thanks in advance.
I think it' just a design matter. Why do you put two fields that have the same information, since the user has account with receiveaddress, adding the user as foreign key will be enough and cleaner, I suggest the following:
class Account(models.Model):
username = models.OneToOneField(User, primary_key=True, unique=True)
receiveaddress = models.CharField(max_length=40, blank=True, null=True, unique=True)
balance = models.DecimalField(max_digits=16, decimal_places=8, default=0)
def __str__(self):
return str(self.username)
class Deposit(models.Model):
amount = models.DecimalField(max_digits=16, decimal_places=8, default=0)
user = models.ForeignKey(User, related_name="deposits")
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
confirmed = models.BooleanField(default=False)
accounted = models.BooleanField(default=False)
def __str__(self):
return str(self.user.account.receiveaddress)
NB: As a convention, models name should be always singular

Categories