How can I remove duplicate objects in an order_by query? - django

>>> a = group.objects.order_by('groupname')
>>> print a
[<group: beginner 593785332>, <group: beginner 903647323>, <group: blbrz 229225098>]
I don't want to have objects with similar goupname, I want to have distinct groupname for each object:
[<group: beginner 593785332>, <group: blbrz 229225098>]
What can I do?
from django.db import models
class accounts(models.Model):
twitterid = models.IntegerField()
credit = models.IntegerField()
activate = models.TextField()
ban = models.TextField(blank=True)
others = models.TextField()
def __unicode__(self):
return u'%s' % (self.twitterid)
class Meta:
ordering = ['twitterid']
class group(models.Model):
groupname = models.TextField()
accounts=models.ForeignKey(accounts)
def __unicode__(self):
return u'%s %s' % (self.groupname, self.accounts)

If your database backend were PostgreSQL, you could do it with a queryset:
a = group.objects.order_by('groupname').distinct('groupname')
Unfortunately you are using SQLite, so you would preferably do it in python :
a = group.objects.order_by('groupname')
groupnames = set()
b = []
for item in a:
if a.groupname not in groupnames:
b.append(a)
groupnames.add(a.groupname)
a = b

Related

how to count total student according to 'course' model in django

I am trying to count the number of the student according to CourseMasterModel.
I did it in MySQL, but I want to in Django.
select cn.course_name,count(st.id) from course_master
cn,semister_master sem,division_master di,student_profile st where
st.division_id = di.id and di.semister_id = sem.id and sem.course_id =
cn.id GROUP BY cn.course_name;
class CourseMasterModel(models.Model):
course_name = models.CharField(max_length=20,unique=True)
total_semister = mod`enter code here`els.SmallIntegerField()
class Meta:
db_table = "course_master"
verbose_name_plural = 'Course (Department)'
verbose_name = "Course"
def __str__(self):
return self.course_name
class SemisterMasterModel(models.Model):
semister = models.SmallIntegerField()
total_div = models.SmallIntegerField()
course = models.ForeignKey(CourseMasterModel,on_delete=models.PROTECT)
class Meta:
db_table = "Semister_master"
verbose_name_plural = 'Semister'
verbose_name = "semister"
def __str__(self):
return "%s - %d" %(self.course.course_name,self.semister)
class DevisionMasterModel(models.Model):
div_name = models.CharField(max_length=2)
semister = models.ForeignKey(SemisterMasterModel,on_delete=models.CASCADE)
class Meta:
db_table = "division_master"
verbose_name_plural = 'Division'
verbose_name = "Division"
def __str__(self):
return "%s - %s - %s"%(self.semister.course.course_name,self.semister.semister,self.div_name)
class StudentProfileModel(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE,related_name="profile")
division = models.ForeignKey('core.DevisionMasterModel',on_delete=models.CASCADE,verbose_name="Course / Semister / Division")
roll_no = models.IntegerField()
enrollment_no = models.IntegerField(unique=True, error_messages={'unique':"This enrollment number has already been registered."})
def __str__(self):
return self.user.username
class Meta:
db_table = "Student_Profile"
You can annotate your CourseMasterModel, like:
from django.db.models import Count
CourseMasterModel.objects.annotate(
nstudents=Count('semistermastermodel__devisionmastermodel__studentprofilemodel')
)
The CourseMasterModels that arise from this QuerySet have an extra attribute .nstudents that contains the number of related StudentProfileModels.
Note: usually the names of Django models have no Model suffix, so CourseMaster instead of CourseMasterModel.
In case you rename the models, the query is:
from django.db.models import Count
CourseMasterModel.objects.annotate(
nstudents=Count('semistermaster__devisionmaster__studentprofile')
)

QuerySet in Django - returns exception

I am trying to understand how exactly query works on Django, i followed the tutorials it´s not working I am not sure what i am doing wrong.
When I run
BeneficientePagar.objects.filter(nome__contains="Joao Pedro")
it returns
"Choices are %s" %s (name, ",".join(available))) django.core.exceptions.FieldError: Cannot resolve keyword "nome into field. Choices are: ID, beneficiente, beneficiente_id,join, join_id, moeda
from django.db import models
# Create your models here.
class Moeda(models.Model):
moeda_ficticia = models.FloatField()
class Join(models.Model):
nome = models.CharField(max_length=150)
nascimento = models.DateField()
cpf = models.IntegerField(primary_key=True)
endereco = models.CharField(max_length=150)
email = models.EmailField()
def __str__(self):
return self.nome
class Beneficiente(models.Model):
ID = models.AutoField(primary_key=True)
nome = models.CharField(max_length=150)
CNPJ = models.IntegerField(max_length = 10)
def __str__(self):
return self.nome
class Favores(models.Model):
ID = models.AutoField(primary_key=True)
favor = models.CharField(max_length=150)
dataInserido = models.DateField()
usuarios = models.ForeignKey(Join)
def __str__(self):
return self.favor
class BeneficientePagar(models.Model):
ID = models.AutoField(primary_key=True)
moeda = models.IntegerField()
beneficiente = models.ForeignKey(Beneficiente)
join = models.ForeignKey(Join)
def __str__(self):
return self.ID
Thanks in advance
If using BeneficientPager, you need to do
BeneficientePagar.objects.filter(beneficient__nome__contains="Joao Pedro")
You are getting the error because nome is a field on Beneficiente, not BeneficientePagar.
You can either do
Beneficiente.objects.filter(nome__contains="Joao Pedro")
which will return a queryset of Beneficientes. Or if you need BeneficientePagar you can query through the foreign key.
BeneficientePagar.objects.filter(beneficiente__nome__contains="Joao Pedro")

IntegrityError : yonetim_ders.ogretim_elemani_id may not be NUL

My models.py:
from django.db import models
class OgretimElemani(models.Model):
adi = models.CharField(max_length=50)
soyadi = models.CharField(max_length=50)
telefonu = models.CharField(max_length = 10 , blank=True)
e_posta_adresi = models.EmailField(blank=True)
def __unicode__(self):
return '%s %s' % (self.soyadi,self.adi)
class Ders(models.Model):
kodu = models.CharField(max_length=10)
adi = models.CharField(max_length=50)
ogretim_elamani = models.ForeignKey(OgretimElemani)
tanimi = models.CharField(max_length=1000,blank = True)
def __unicode__(self):
return '%s %s %s' % (self.kodu,self.adi,self.ogretim_elamani)
class Ogrenci(models.Model):
numarasi = models.IntegerField()
adi = models.CharField(max_length=50)
soyadi = models.CharField(max_length=50)
aldigi_dersler = models.ManyToManyField(Ders)
def __unicode__(self):
return '%s %s %s' % (self.soyadi,self.adi,self.aldigi_dersler)
Django shell :
>>>ders1=Ders(kodu='KIM101', adi='Kimya-1')
>>>ders1.ogretim_elemani=OgretimElemani[0]
>>>ders1.save()
IntegrityError : yonetim_ders.ogretim_elemani_id may not be NULL
You must assign a OgretimElemani instance that has already been saved to the database.
For example, the following should work.
>>> ders1 = Ders(kodu='KIM101', adi='Kimya-1'
>>> ogretim_elemani = OgretimElemani.objects.all()[0] # fetch the first one from the database
>>> ders1.ogretim_elemani = ogretim_elemani
>>>ders1.save()
Your example is not very clear because you use OgretimElemani[0]. You shouldn't reuse the variable name OgretimElemani, it makes the code confusing.

django sort on calculated model field -- can I use .extra?

I have the model below. I want to order by percent_vote. I know I could calculate the value and save to a new field in the model, but I prefer not to go that way. Is there some way to use .extra method to do this?
Django 1.6, sqlite3
class HallOfFame(models.Model):
player = models.ForeignKey(Master)
year = models.IntegerField(db_index=True)
voted_by = models.CharField(max_length=30)
ballots_cast = models.IntegerField()
votes_needed = models.IntegerField()
votes_received = models.IntegerField(db_index=True)
inducted = models.BooleanField(db_index=True)
category = models.CharField(max_length=30, db_index=True)
needed_note = models.CharField(max_length=75, blank=True, null=True)
def __unicode__(self):
return "%s %s" % (self.player.name_first, self.player.name_last)
def percent_vote(self):
try:
return self.votes_received/float(self.ballots_cast)
except ZeroDivisionError:
return 0
Yes, it seems you can do something like this, but this may depend on the your database backend ( this should work for PostgreSQL ):
q = HallOfFame.objects.extra(select={'percent_vote_integer': "votes_received/ballots_cast", 'percent_vote_remainder': "votes_received%ballots_cast"})
q = q.extra(order_by = ['percent_vote_integer', percent_vote_remainder])
I ended up solving this issue with the code below. #Emil Davtyan's answer didn't work for me, and I wanted to figure out a more general solution.
def home(request):
hall = HallOfFame.objects.filter(inducted=True, voted_by='BBWAA')
hall_sorted = sorted(hall, key=lambda member: member.percent_vote, reverse=True)[:20]
return render_to_response('top_lists.html', {'hall': hall_sorted })
the model has this:
#property
def percent_vote(self):
try:
return float(self.votes_received)/self.ballots_cast
except ZeroDivisionError:
return 0

How to always filter on a field on objects requests

I have two models :
class Album(models.Model):
#Attributes
title = models.CharField(max_length=200)
displayed = models.BooleanField()
created_on = models.DateTimeField(auto_now_add=True)
class Photos(models.Model):
#Attributes
title = models.CharField(max_length=500)
link = models.CharField(max_length=500)
album = models.ForeignKey(Album, unique=False, verbose_name=_('album'))
def upload_path(self, filename):
return 'upload/photos/%s/%s' % (self.id, filename)
def upload_path_thumbnail(self, filename):
return 'upload/photos/%s/%s' % (self.id, "thumnail_" +filename)
thumbnail = models.ImageField(upload_to=upload_path_thumbnail)
photo = models.ImageField(upload_to=upload_path)
created_on = models.DateTimeField(auto_now_add=True)
displayed = models.BooleanField()
And I want to force, when i select Photos, to always filter on displayed=1.
Thank you
Use a custom manager:
class DisplayedPhotoManager(models.Manager):
def get_queryset(self):
return super(DisplayedPhotoManager, self).get_queryset().filter(displayed=1)
class Photos(models.Model):
objects = DisplayedPhotoManager()
...
this will override standard "objects" manager (which can be dangerous).
A nicer pattern is often:
class DisplayedPhotoManager(models.Manager):
def get_queryset(self):
return super(DisplayedPhotoManager, self).get_queryset().filter(displayed=1)
class Photos(models.Model):
objects = models.Manager()
displayed = DisplayedPhotoManager()
...
and use 'displayed' instead of 'objects':
Photo.displayed.all()