django-orm

Does a Postgresql dump create sequences that start with - or after - the last key?

笑着哭i 提交于 2019-12-07 09:59:32
I recently created a SQL dump of a database behind a Django project, and after cleaning the SQL up a little bit was able to restore the DB and all of the data. The problem was the sequences were all mucked up. I tried adding a new user and generated the Python error IntegrityError: duplicate key violates unique constraint . Naturally I figured my SQL dump didn't restart the sequence. But it did: DROP SEQUENCE "auth_user_id_seq" CASCADE; CREATE SEQUENCE "auth_user_id_seq" INCREMENT 1 START 446 MAXVALUE 9223372036854775807 MINVALUE 1 CACHE 1; ALTER TABLE "auth_user_id_seq" OWNER TO "db_user"; I

Django exclude from annotation count

两盒软妹~` 提交于 2019-12-07 03:30:25
问题 I have following application: from django.db import models class Worker(models.Model): name = models.CharField(max_length=60) def __str__(self): return self.name class Job(models.Model): worker = models.ForeignKey(Worker) is_completed = models.BooleanField() I want to annotate Workers query with count of completed jobs. I'll try to do it with following script: from myapp.models import Worker, Job from django.db.models import Count w = Worker.objects.create(name='Worker1') Job.objects.create

Translating query with JOIN expressions and a generic relation to Django ORM

早过忘川 提交于 2019-12-07 02:48:28
class Business(models.Model): manager = models.ForeignKey(User, on_delete=models.CASCADE) #... class Event(models.Model): business = models.ForeignKey(Business, on_delete=models.CASCADE) text = models.TextField() when = models.DateTimeField() likes = GenericRelation('Like') class Like(models.Model): person = models.ForeignKey(User, on_delete=models.CASCADE) content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = GenericForeignKey('content_type', 'object_id') date = models.DateTimeField(auto_now=True) So I have this structure in models.py . Here

django-orm : How to update one-to-one relation field value

与世无争的帅哥 提交于 2019-12-07 02:31:24
models.py class Area(models.Model): area_name = models.CharField(max_length=255, null=False, blank=False) description = models.TextField(null=False, blank=False) class AreaPoint(models.Model): x_axis = models.FloatField(default=0.0) y_axis = models.FloatField(default=0.0) area = models.OneToOneField(Area,primary_key=True,on_delete=models.CASCADE) I try two method , but both fail ,please guide me. thank you # first method : # Area.objects.filter(id=304).update(area_name="today is 1", description="today is 1", areapoint__x_axis=111,areapoint__y_axis=222) # error : Area has no field named

Filtering Django Query by the Record with the Maximum Column Value

余生颓废 提交于 2019-12-06 22:36:16
问题 Is there an easy way to filter a Django query based on which record has a max/min value in a column? I'm essentially asking these questions, but in the specific context of Django's ORM. e.g. Say I have a model designed to store the historical values of everyone's phone numbers. class Person(models.Model): name = models.CharField(max_length=100) phone = models.CharField(max_length=100) created = models.DateTimeField(auto_now_add=True) with the records: Person(name='Jim',phone='123-456-9870',

Logging Django SQL queries with DEBUG set to False

别来无恙 提交于 2019-12-06 22:09:08
问题 I know that it is possible to get all the SQL queries that were run for the current request/response when DEBUG is on by looking at connection.queries. The django-debug-toolbar also helps a lot on development. The problem is that my production server is under high load and I would like to log the queries that are being executed for each view so I can optimize the pages that are creating more queries first. Is it possible to do that without modifying my database driver? 回答1: In Django 1.3, I

django - get list of objects by filtering a list of objects

馋奶兔 提交于 2019-12-06 14:22:32
I am creating a user activity streams. models for activity: class Activity(models.Model): actor = models.ForeignKey(User) action = models.CharField(max_length=100) content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey('content_type', 'object_id') pub_date = models.DateTimeField(auto_now_add=True, auto_now=False) model for Relationship: class Person(models.Model): user = models.OneToOneField(User) relationships = models.ManyToManyField('self', through='Relationship', symmetrical=False, related_name='related_to')

Django GenericIPAddress field is not validating input

旧街凉风 提交于 2019-12-06 13:52:06
问题 Hi I have the following Django model class AccessPointIPAddress(models.Model): '''Model for storing AccessPoint IP Addresses.''' ap = models.ForeignKey(AccessPoint, related_name='ip_addresses') ip_address = models.GenericIPAddressField(protocol='IPv4') datetime = models.DateTimeField(auto_now_add=True) class Meta: ordering = ['datetime'] get_latest_by = 'datetime' And I am assuming that django's GenericIPAddressField does some string validation that a string is indeed a valid IP Address. I

Save the user from one model to the another model

风流意气都作罢 提交于 2019-12-06 11:37:06
What I want to do is, whenever I create a new message, I want the sender of the Message to be added to the user of that particular Thread (the Message its relating to). How do I do that? Can it be done by overriding the save method? I can do it in the views.py, but I was hoping it would be better if I can add it in the models.py itself. Any help will be very grateful. Thank you! class Thread(models.Model): user = models.ManyToManyField(User) is_hidden = models.ManyToManyField(User, related_name='hidden_thread', blank=True) def __unicode__(self): return unicode(self.id) class Message(models

Django: Custom Save method for Many-to-Many relation

余生颓废 提交于 2019-12-06 10:53:25
问题 I need to set custom save and delete methods on a Many-to-Many relation. I tried specifying a model with the "through" attribute but this over-complicated my code and introduced some problems. I don't need any extra field on the Many-to-Many model, just custom save and delete methods. Is it possible to accomplish this without specifying the "through" attribute? Here's code: class Order(BaseDate): #lots of fields relateds = models.ManyToManyField('RelatedProduct', verbose_name=_('related