In my Django model I have 2 field. When I execute below code it just prints the resolution field. How can I get the all fields data in a list?
x = ResolutionsModel.objects.all()
for i in x:
print(i)
models.py
class ResolutionsModel(models.Model):
resolution = models.TextField(max_length=30,blank=True)
act_abbreviation = models.TextField(max_length=30)
def __str__(self):
return self.resolution
So your model says that to represent an instance of itself as a string it should use the value of resolution. So by printing an instance, that's what you're getting - the value of resolution.
If you pass your queryset to a template you could output the values from all the fields.
For the purposes of your test in python you'd have to specifically include each field;
x = ResolutionsModel.objects.all()
for i in x:
print(i.resolution)
print(i.act_abbreviation)
If you actually want to get data in a list, you might want to read about how to use values_list on a queryset; https://docs.djangoproject.com/en/4.0/ref/models/querysets/#values-list
For the purpose of getting to know django you could also adapt your str method;
def __str__(self):
return f"{self.resolution}, {self.act_abbreviation}"
In your case:
x = ResolutionsModel.objects.all()
The x here is a query set, which returns a bunch of entries from data base, each entry is a database entry:
for i in x:
print(i)
i # is a database entry, you can access i.resolution & i.act_abbreviation at each loop.
In the end, everything is an object.
Related
I have I think kind of a tricky question in Django and it's orm.
This does not work :
cartitemproduct_in_cart_session.get().quantity+=1
cartitemproduct_in_cart_session.get().save()
If I check just after that the value of cartitemproduct_in_cart_session.get().quantity, it wasn't updated
This works :
cartitem_session=cartitemproduct_in_cart_session.get()
cartitem_session.quantity+=1
cartitem_session.save()
The value was updated
But why ?
(cartitemproduct_in_cart_session is a queryset, result of a filter, but I think it doesn't matter : cartitemproduct_in_cart_session=cart_session.cartitem_set.filter(product__slug=cartitem.product.slug) )
I am guessing that somehow, when I do cartitemproduct_in_cart_session.get().quantity, the field quantity becomes a new attributes of cartitemproduct_in_cart_session.get() and isn't linked anymore to the field in the database, but I don't understand why ...
Why do you need to first assign an instance of a model to a name, in order to update the fields of that instance ?
cartitemproduct_in_cart_session.get().quantity+=1
cartitemproduct_in_cart_session.get().save()
is equivalent to:
x = cartitemproduct_in_cart_session.get()
x.quantity += 1
y = cartitemproduct_in_cart_session.get()
y.save()
# note that x and y are different objects with different memory addresses
while
cartitem_session=cartitemproduct_in_cart_session.get()
cartitem_session.quantity+=1
cartitem_session.save()
is equivalent to:
x = cartitemproduct_in_cart_session.get()
x.quantity += 1
x.save()
I'am trying to create a model unittest for a ManyToMany relationship.
The aim is to check, if there is the right category saved in the table Ingredient.
class IngredientModelTest(TestCase):
def test_db_saves_ingredient_with_category(self):
category_one = IngredientsCategory.objects.create(name='Food')
first_Ingredient = Ingredient.objects.create(name='Apple')
first_Ingredient.categories.add(category_one)
category_two = IngredientsCategory.objects.create(name='Medicine')
second_Ingredient = Ingredient.objects.create(name='Antibiotics')
second_Ingredient.categories.add(category_two)
first_ = Ingredient.objects.first()
self.assertEqual('Apple', first_.name)
self.assertEqual(first_.categories.all(), [category_one])
self.assertEqual(first_, first_Ingredient)
for self.asserEqual(first_.categories.all(), [category_one]) in the second last row I get this weird assert:
AssertionError: [<IngredientsCategory: Food>] != [<IngredientsCategory: Food>]
I tried many other different ways, but none of it worked. Does any one suppose how I can get the information of first_.categories.all() to compare it with something else?
That'll be because they're not equal - one is a QuerySet, the other is a list - they just happen to have the same str representations.
You could either cast the QuerySet to a list with list(first_.categories.all()), or a possible solution for this situation may be:
self.assertEqual(first_.categories.get(), category_one)
As mentioned by the title, in Django:
Say I have a model name QuestionRecord, with two fields: full_score and actual_score.
I want to realize the SQL:
select * from QuestionRecord as QR where QR.full_score!=QR.actual_score.
Maybe using raw sql is OK, but I want to implement it like this:
class QuestionRecord_QuerySet(models.query.QuerySet):
def incorrect(self):# Find out those whose full_score and actual_score are not equal
return self.filter(...) # **What should I write here??**
class QuestionRecord_Manager(models.Manager):
def get_query_set(self):
return QuestionRecord_QuerySet(self.model)
class QuestionRecord(models.Model):
objects = QuestionRecord_Manager()
Is there any way to do it?
Sure, that's what the "F" object is for!
from django.db.models import F
# snip
return self.exclude(full_score = F('actual_score'))
Use QuerySet.exclude here, as you want to not get the results that match.
If you really want to use QuerySet.filter, you can use a negated "Q" object: self.filter(~Q(full_score = F('actual_score'))).
I have a model with a reference property, eg:
class Data(db.Model):
x = db.IntegerProperty()
class Details(db.Model):
data = db.ReferenceProperty(reference_class = Data)
The data reference can be None.
I want to fetch all Details entities which have valid data, ie for which the reference property is not None.
The following works:
Details.all().filter('data !=', None).fetch(1000)
However, according to the documentation on queries, a != query will actually perform two queries, which seems unnecessary in this case. Is != optimised to only perform one query when used with None?
Alternatively, this post mentions that NULL always sorts before valid values. Therefore the following also appears to work:
Details.all().filter('data >', None).fetch(1000)
Although this would only do one query, using > instead of != makes the intent of what it is doing less obvious.
As a third option, I could add an extra field to the model:
class Details(db.Model):
data = db.ReferenceProperty(reference_class = Data)
has_data = db.BooleanProperty()
As long as I keep has_data synchronised with data, I could do:
Details.all().filter('has_data =', True).fetch(1000)
Which way would be best?
Thanks.
I would advise you to use the extra model field. This is more flexible, since it also allows you to query for Details that have no Data references. In addition, queries can only have one inequality filter, so you're better off saving this inequality filter for another property where inequality makes more sense, such as integer properties.
To make sure the flag is always updated, you can add a convenience function to Details, like so:
class Details(db.Model):
data = db.ReferenceProperty(reference_class=Data)
has_data = db.BooleanProperty(default=False)
def add_data(self, data):
""" Adds data"""
if not data: return
self.has_data = True
self.data = data
return self.put()
Is it possible to filter a Django queryset by model property?
i have a method in my model:
#property
def myproperty(self):
[..]
and now i want to filter by this property like:
MyModel.objects.filter(myproperty=[..])
is this somehow possible?
Nope. Django filters operate at the database level, generating SQL. To filter based on Python properties, you have to load the object into Python to evaluate the property--and at that point, you've already done all the work to load it.
I might be misunderstanding your original question, but there is a filter builtin in python.
filtered = filter(myproperty, MyModel.objects)
But it's better to use a list comprehension:
filtered = [x for x in MyModel.objects if x.myproperty()]
or even better, a generator expression:
filtered = (x for x in MyModel.objects if x.myproperty())
Riffing off #TheGrimmScientist's suggested workaround, you can make these "sql properties" by defining them on the Manager or the QuerySet, and reuse/chain/compose them:
With a Manager:
class CompanyManager(models.Manager):
def with_chairs_needed(self):
return self.annotate(chairs_needed=F('num_employees') - F('num_chairs'))
class Company(models.Model):
# ...
objects = CompanyManager()
Company.objects.with_chairs_needed().filter(chairs_needed__lt=4)
With a QuerySet:
class CompanyQuerySet(models.QuerySet):
def many_employees(self, n=50):
return self.filter(num_employees__gte=n)
def needs_fewer_chairs_than(self, n=5):
return self.with_chairs_needed().filter(chairs_needed__lt=n)
def with_chairs_needed(self):
return self.annotate(chairs_needed=F('num_employees') - F('num_chairs'))
class Company(models.Model):
# ...
objects = CompanyQuerySet.as_manager()
Company.objects.needs_fewer_chairs_than(4).many_employees()
See https://docs.djangoproject.com/en/1.9/topics/db/managers/ for more.
Note that I am going off the documentation and have not tested the above.
Looks like using F() with annotations will be my solution to this.
It's not going to filter by #property, since F talks to the databse before objects are brought into python. But still putting it here as an answer since my reason for wanting filter by property was really wanting to filter objects by the result of simple arithmetic on two different fields.
so, something along the lines of:
companies = Company.objects\
.annotate(chairs_needed=F('num_employees') - F('num_chairs'))\
.filter(chairs_needed__lt=4)
rather than defining the property to be:
#property
def chairs_needed(self):
return self.num_employees - self.num_chairs
then doing a list comprehension across all objects.
I had the same problem, and I developed this simple solution:
objects = [
my_object
for my_object in MyModel.objects.all()
if my_object.myProperty == [...]
]
This is not a performatic solution, it shouldn't be done in tables that contains a large amount of data. This is great for a simple solution or for a personal small project.
PLEASE someone correct me, but I guess I have found a solution, at least for my own case.
I want to work on all those elements whose properties are exactly equal to ... whatever.
But I have several models, and this routine should work for all models. And it does:
def selectByProperties(modelType, specify):
clause = "SELECT * from %s" % modelType._meta.db_table
if len(specify) > 0:
clause += " WHERE "
for field, eqvalue in specify.items():
clause += "%s = '%s' AND " % (field, eqvalue)
clause = clause [:-5] # remove last AND
print clause
return modelType.objects.raw(clause)
With this universal subroutine, I can select all those elements which exactly equal my dictionary of 'specify' (propertyname,propertyvalue) combinations.
The first parameter takes a (models.Model),
the second a dictionary like:
{"property1" : "77" , "property2" : "12"}
And it creates an SQL statement like
SELECT * from appname_modelname WHERE property1 = '77' AND property2 = '12'
and returns a QuerySet on those elements.
This is a test function:
from myApp.models import myModel
def testSelectByProperties ():
specify = {"property1" : "77" , "property2" : "12"}
subset = selectByProperties(myModel, specify)
nameField = "property0"
## checking if that is what I expected:
for i in subset:
print i.__dict__[nameField],
for j in specify.keys():
print i.__dict__[j],
print
And? What do you think?
i know it is an old question, but for the sake of those jumping here i think it is useful to read the question below and the relative answer:
How to customize admin filter in Django 1.4
It may also be possible to use queryset annotations that duplicate the property get/set-logic, as suggested e.g. by #rattray and #thegrimmscientist, in conjunction with the property. This could yield something that works both on the Python level and on the database level.
Not sure about the drawbacks, however: see this SO question for an example.