YourModel.objects.filter(B='something').delete()
or for empty foreign key:
YourModel.objects.filter(B__isnull=True).delete()
Вопрос
I have a model of object Obj created in django with some fields (A,B,C) While creating some of the objects in data base people did not fill B field. Now I want to filter objects by B, like: for obj in all_Obj: if obj.B = "something": obj.delete()
But I am getting this error: users.models.DoesNotExist: Profile matching query does not exist for the line: if c.main_name.user == user[0]: The same error I am getting when writing if not c.main_name.user: So how can I solve this? I can not rewrite this field because I can not address it and I can not filter by this field to delete some entries I don't need
Thanks a lot for help.. :)
In the request c is concept:
class Concept(models.Model):
partial_url = models.CharField(max_length=200, unique=True)
insert_links = models.BooleanField(default=True)
is_category = models.BooleanField(default=False)
date_modified = models.DateTimeField(auto_now=True)
main_name = models.ForeignKey('Literal', related_name='concepts', blank=True, null=True,
unique=True)
category = models.ForeignKey('self', blank=True, null=True)
deletion_requests = generic.GenericRelation(DeletionRequest,
content_type_field='entry_ct', object_id_field='entry_id')
And c.main_name is Literal:
class Literal(models.Model):
"""Represents particular string literal of the concept"""
name = models.CharField(max_length=200)
norm_name = models.CharField(max_length=200)
concept = models.ForeignKey(Concept)
user = models.ForeignKey(Profile)
date_modified = models.DateTimeField(auto_now=True)
Unexisting field - user
Решение
YourModel.objects.filter(B='something').delete()
or for empty foreign key:
YourModel.objects.filter(B__isnull=True).delete()