I have models like this:
class A(Model):
....
class B(Model):
a = ForeignKey(A, related_name="bbb")
class C(Model)
b = ForeignKey(B, related_name="ccc")
file = FileField( ... , null=True, blank=True)
In template or in view I need a mark A row, if some C object related to A has file=None (null) .
Thanks.
If I understand correctly, try this:
for a in A.objects.all():
for b in a.bbb.all():
for c in b.ccc.filter(file__isnull=True):
a.has_c_with_null_file = True
a.save()
OR
c_without_file = C.objects.filter(file__isnull=True)
for c in c_without_file:
c.b.a.has_c_with_null_file = True
c.b.a.save()
OR
A.objects.filter(b__c__file__isnull=True).update(has_c_with_null_file=True)
If you leave off the related name, use b_set and c_set.
Related
BEFORE :
class Person(ndb.Model):
name = ndb.StringProperty()
age = ndb.StringProperty()
other_details = ndb.StructuredProperty(OtherDetails, 'othrdtl')
class OtherDetails(ndb.Model):
success = ndb.StringProperty()
qr_code = ndb.TextProperty()
AFTER:
class Person(ndb.Expando):
pass
rows_to_be_updated = []
for person in Person.all():
person.age = int(person.age)
rows_to_be_updated.append(person)
if len(rows_to_be_updated)>0:
ndb.put_multi_async(rows_to_be_updated)
#"When the above line is executed i am getting error"
Very AFTER:
class Person(db.Model):
name = db.StringProperty()
age = db.IntegerProperty()
other_details = ndb.StructuredProperty(OtherDetails, 'othrdtl')
As per the datastore document, the TextProperty is unindexed by default. What is the reason for the error? I have tried making explicit Indexed=False (ndb.TextProperty(indexed= False)) but didn't work.
Instead of trying to go through an expando model, you might consider using a different name in your database than in your model.
E.g. after
class Person(ndb.Model):
name = ndb.StringProperty()
string_age = ndb.StringProperty('age')
age = ndb.IntegerProperty('int_age')
other_details = ndb.StructuredProperty(OtherDetails, 'othrdtl')
Then you could use a hook to ensure that Person.age is always set, e.g.
class Person(ndb.Model):
#classmethod
def _post_get_hook(cls, key, future):
p = future.get_result()
if p and not p.age:
p.age = int(p.string_age)
I have this code :
entryA = myTable1.objects.all().first()
entryB = copy.deepcopy(entryA)
But the problem is I would want to save entryB but if I do entryB.save() I will have a new entry in myTable1 whereas I want to have a new entry in myTable2 which contains the same fields.
Could you help me please ?
Thank you
Here is my model :
class myTable1(models.Model):
number = models.BooleanField(default=True)
date = models.DateField(default=None)
class myTable2(models.Model):
number = models.BooleanField(default=True)
date = models.DateField(default=None)
You'll need to copy the properties of one object to another object and then use the create method to create a new instance.
class myTable1(models.Model):
number = models.BooleanField(default=True)
date = models.DateField(default=None)
class myTable2(models.Model):
number = models.BooleanField(default=True)
date = models.DateField(default=None)
entryA = myTable1.objects.all().first()
entryB = myTable2.objects.create(number=entryA.number, date=entryA.date)
Or if you want myTable2 to have unique data, you can do:
entryA = myTable1.objects.all().first()
entryB, created = myTable2.objects.get_or_create(number=entryA.number, date=entryA.date)
I want to get the parent class values with each child values? How can I identify child objects to fetch?
I have the Django model structure like this.
class Category(models.Model):
name = models.CharField(max_length=80)
class Place(models.Model):
name = models.CharField(max_length=50)
address = models.CharField(max_length=80)
category = models.ForeignKey('Category')
class PizzaRestaurant(Place):
serves_hot_dogs = models.BooleanField(default=False)
serves_pizza = models.BooleanField(default=False)
class PastaRestaurant(Place):
extra = models.CharField(max_length=80)
When we do operation we may save the object like below. And it saved into the db as i expected. two entry in the Place table and each entry in each child object table.
a = Category()
a.name = "pasta"
b = Category()
b.name = "pizza"
a.save()
b.save()
x = PastaRestaurant()
x.address = "Pasta Address"
x.name = "Pastamonia"
x.extra = "some extra"
x.category = a
y = PizzaRestaurant()
y.address = "Pizza Address"
y.name = "Dominos"
y.serves_hot_dogs = 1
y.serves_pizza = 0
y.category = b
x.save()
y.save()
Now I need to access the like this
p = Place.objects.get(id=1)
How can I know, which objects/attributes belongs to the place objects?
So when I fetch the place with common attributes and should be able get the corresponding child objects values also.
Or any other model design work for my need?
If you want to access the child model's attributes you need to fetch it as that model, i e PizzaRestaurant or PastaRestaurant, otherwise you will only get a Place object.
If you need to get all Places regardless of subclass take a look at InheritanceManager from django-model-utils. Using this you can implement overloaded operations to perform subclass-specific actions.
django-polymorphic does this beautifully, improving the abilities to work with model inheritance like so:
from polymorphic.models import PolymorphicModel
class Place(PolymorphicModel):
...
class PizzaRestaurant(Place):
...
class PastaRestaurant(Place:
...
>>> some_place = Place.objects.create(name="Walmart")
>>> some_pizza_place = PizzaRestaurant.objects.create(name="Slice King", address="101 Main St., Bismarck, ND", category = Category.objects.first(),serves_pizza=True)
>>> some_pizza_place.instance_of(PizzaPlace)
True
>>> PizzaRestaurant.objects.all()
queryset<['Slice King',]>
>>> Place.objects.all()
queryset<['Walmart', 'Slice King',]>
I have two models:
class ModManager(models.Manager):
def myfilter(self, options = dict()):
if options.has_key('not_image'):
kwargs['image__isnull'] = False
return self.filter(**kwargs)
class Model_1(models.Model):
...
objects = MyManager()
class Model_2(models.Model):
something = models.ForeignKey(Model_1)
something_else = ...
...
How to get all data from Model_2 related to Model_1 in MyManager? I want to get one query. I have so far:
in Model_1:
def get_model_2(self):
self.model_2_objs = self.model_2_set.all()
But it generates many queries when I calling get_model_2 function.
i having problem with this:
model.py (1)
class Profession(models.Model):
user= models.ForeignKey(User,unique=True)
principal_area = models.ForeignKey(Area,verbose_name='Area principal',related_name='area_principal')
others_areas = models.ManyToManyField(Area)
model.py (2)
class Area(models.Model):
area = models.CharField(max_length=150,unique=True)
slug = models.SlugField(max_length=200)
activa = models.BooleanField(default=True)
In Model 1 i have a field "principal_area" and other "others_areas".
How ill list all professional where "principal_area" OR "others_areas" are in Area model from my views?
Sorry if im not too clear
Take a look at Django's Q objects. Here is an example of how you could go about this:
area = Area.objects.get(**conditions)
Profession.objects.filter(
Q(principal_area = area) | Q(others_areas__in = [area])
)