In a django project i have created a model class having a foreign key.
model class
When i tried to get objects in a variable by classname.objects.get(parameters=value).
assigning objects into a variable
Now when to display the object's attributes html by django template.
iterating through objects
now when i run the program i am getting error of 'Bid' object is not iterable.
how to correct this code to working?
thankyou
filter() will always give you a QuerySet, even if only a single object matches the query.
If you know there is only one object that matches your query, you can use the get() method on a Manager which returns the object directly.
That means you .get() will only return a single element which is not iterable.
So, you could use .filter instead of .get() in your view (listingpage) and fix it.
bids = Bid.objects.filter(title = title_id)
Related
I thought I understood that get_or_create() returns a tuple, therefore assigning [0] at the end should pick the first object in the query result. However, it raises an error like the following:
MultipleObjectsReturned at /admin/hr/clientcontact/add/
get() returned more than one ClientDepartment -- it returned 4!
(I do have 4 objects in the ClientDepartment model.)
Below is the code for ClientContact model where I used get_or_create()[0].
class ClientContact(Person):
def get_default_client_department():
return ClientDepartment.objects.get_or_create()[0].id
department = models.ForeignKey(
'ClientDepartment',
on_delete=models.RESTRICT,
default=get_default_client_department,
)
def company(self):
return self.department.company
I would appreciate any help and the explanation.
Thank you.
You need to add keywords in get_or_create function to get/create the required entry as below:
ClientDepartment.objects.get_or_create(name='name')[0].id
Provided that name is a field of ClientDepartment model. You can replace it with keywords based on your model.
Furthermore, get_or_create() is combination of get() and create().
get() returns a single object. As you have not mentioned any keywords, therefore it tried to fetch all records thus throwing exception.
so I've got this model:
class Action(models.Model):
d_changes = ArrayField(models.FloatField(), default=list(), verbose_name='D Changes')
w_changes = ArrayField(models.FloatField(), default=list(), verbose_name='A Changes')
And when I want to create a migration or a fixture I always receive the following warning for both fields:
backend.Action.d_changes: (postgres.E003) ArrayField default should be a callable instead of an instance so that it's not shared between all field instances.
HINT: Use a callable instead, e.g., use `list` instead of `[]`.
For my migrations its not such a big deal, since everything still works fine. But when I try to create a fixture of my db, my .json File always ends up with this bit in the very top of my .json file:
System check identified some issues:
WARNINGS:
[33;1mbackend.Action.d_changes: (postgres.E003) ArrayField default should be a callable instead of an instance so that it's not shared between all field instances.
HINT: Use a callable instead, e.g., use `list` instead of `[]`.[0m
[33;1mbackend.Action.w_changes: (postgres.E003) ArrayField default should be a callable instead of an instance so that it's not shared between all field instances.
HINT: Use a callable instead, e.g., use `list` instead of `[]`.[0m
Which breaks my .json file and thus I cannot use loaddata, as I always receive a DeserializationError(), so I have to manually remove that part.
So what exactly is wrong with the model fields? I mean I'm literally using default=list() which is a callable?
Thanks for the help :)
You have to do this:
class Action(models.Model):
d_changes = ArrayField(models.FloatField(), default=list, verbose_name='D Changes')
w_changes = ArrayField(models.FloatField(), default=list, verbose_name='A Changes')
list() is not callable list is callable. because list() has been already called.
I'm trying to use postgresql ArrayField in my django project setting it a default, but I can't get rid of the warning ArrayField default should be a callable instead of an instance so that it's not shared between all field instances. HINT: Use a callable instead, e.g. use list instead of []
This is my code:
def get_default_subscriptions():
return 'Noticias, Promociones, Pagos, Compras'.split(', ') # this returns a list
class myModel(models.model):
# other fields
subscriptions = ArrayField(models.CharField(max_length=200), default=get_default_subscriptions()) # this is a callable, so, why the warning?
Docs say about this: If you give the field a default, ensure it’s a callable such as list (for an empty default) or a callable that returns a list (such as a function). Incorrectly using default=[] creates a mutable default that is shared between all instances of ArrayField.
Can you give me an idea of what am I doing wrong?
Use default=get_default_subscriptions (without paranthesis) instead of default=get_default_subscriptions()
To add a bit of clarification to Arakkal's answer, a callable is a function or bound method. What you passed was the return of a function call and not the function itself.
You actually called get_default_subscriptions inline instead of giving Django the function itself so it can call it when it needs to.
I am trying to dynamically add an attribute to at runtime using the following snippets of code:
View
...
for appellation in queryset:
if appellation.id in used_id_set:
appellation.is_used_flag()
print(appellation.is_used)
# prints true as expected
else:
appellation.is_not_used_flag()
first = queryset.first()
print(first.is_used)
# prints AttributeError: 'Appellation' object has no attribute 'is_used'
In Model
...
def is_used_flag(self):
self.is_used = True
def is_not_used_flag(self):
self.is_used = False
Why does it work correctly when in the loop but when I try to retrieve the attribute from an instance after it does not work? I have run into the same issue using setattr, appellation.is_used = True and modifying __dict__. Also is there a better way to do this?
I have referenced these posts:
Why can't you add attributes to object in python? I do have a dict but it does not seem to "persist" after the loop
How to assign a new class attribute via __dict__? Same issue as mentioned above
Dynamically defining instance fields in Python classes Same as above
Update
Since both answers mention similar things, I should clarify what my intentions are. I do not want to actually persist the value in the DB. I just want to serialize it and use it in the front end.
The Queryset API in django (often) returns other querysets, which are in turn evaluated by accessing the database. By doing queryset.first() you're executing another database call, where your attributes have not been yet set.
If you need to save this is_used flag between querysets (persist the change), I suggest you add a BooleanField on your model, or perhaps find another way to do what you want, as in memory attributes will not get returned by using the queryset API.
If you want the change to persist you will need to call self.save() after setting is_used, assuming that is_used is a field on the Appellation model.
models.py
from django.db import models
class Appellation(models.Model):
# ... rest of code ...
is_used = models.BooleanField(default=False)
def is_used_flag(self):
self.is_used = True
self.save()
def is_not_used_flag(self):
self.is_used = False
self.save()
Note that Django instances are still Python objects so adding an attribute dynamically will work in the same way, this is why it prints True as expected in the code you provided.
In django admin, I have a model in which there are several objects. Now in admin, I have the link to Mymodel. If i click that, I get a list, all of which say
Mymodel object
If I need to find a particular record from the table, then I simply have to search the whole list. How can I change the setting so that instead of MyModel object I get to see an attribute, say name of that particular object??
You should define __unicode__ method in your model class:
def __unicode__(self):
return self.name # to display name attribute
From django docs:
The __unicode__() method is called whenever you call unicode() on an
object. Django uses unicode(obj) (or the related function, str(obj))
in a number of places. Most notably, to display an object in the
Django admin site and as the value inserted into a template when it
displays an object. Thus, you should always return a nice,
human-readable representation of the model from the __unicode__()
method.