django-orm

Saving Many To Many data via a modelform in Django

狂风中的少年 提交于 2019-11-26 10:51:50
问题 I have a problem saving many to many fields from a form. Here are my models: class TextIssue(models.Model): Issue = models.CharField(max_length=150, unique=True) def __unicode__(self): return self.Issue class PadIssue(models.Model): Issue = models.CharField(max_length=150, unique=True) def __unicode__(self): return self.Issue class PHIssue(models.Model): Data = models.ForeignKey(Data) TextIssue = models.ManyToManyField(TextIssue, blank=True, null=True) PadIssue = models.ManyToManyField

Django self-referential foreign key

对着背影说爱祢 提交于 2019-11-26 10:36:28
问题 I\'m kind of new to webapps and database stuff in general so this might be a dumb question. I want to make a model (\"CategoryModel\") with a field that points to the primary id of another instance of the model (its parent). class CategoryModel(models.Model): parentId = models.ForeignKey(CategoryModel) How do I do this? Thanks! 回答1: You can pass in the name of a model as a string to ForeignKey and it will do the right thing. So: parentId = models.ForeignKey("CategoryModel") Or you can use the

How to rename items in values() in Django?

自作多情 提交于 2019-11-26 10:19:25
问题 I want to do pretty much the same like in this ticket at djangoproject.com, but with some additonal formatting. From this query >>> MyModel.objects.values(\'cryptic_value_name\') [{\'cryptic_value_name\': 1}, {\'cryptic_value_name\': 2}] I want to get something like that: >>> MyModel.objects.values(renamed_value=\'cryptic_value_name\') [{\'renamed_value\': 1}, {\'renamed_value\': 2}] Is there another, more builtin way or do I have to do this manually? 回答1: It's a bit hacky, but you could use

Why is iterating through a large Django QuerySet consuming massive amounts of memory?

送分小仙女□ 提交于 2019-11-26 10:12:34
问题 The table in question contains roughly ten million rows. for event in Event.objects.all(): print event This causes memory usage to increase steadily to 4 GB or so, at which point the rows print rapidly. The lengthy delay before the first row printed surprised me – I expected it to print almost instantly. I also tried Event.objects.iterator() which behaved the same way. I don\'t understand what Django is loading into memory or why it is doing this. I expected Django to iterate through the

Select DISTINCT individual columns in django?

醉酒当歌 提交于 2019-11-26 09:22:17
问题 I\'m curious if there\'s any way to do a query in Django that\'s not a \" SELECT * FROM... \" underneath. I\'m trying to do a \" SELECT DISTINCT columnName FROM ... \" instead. Specifically I have a model that looks like: class ProductOrder(models.Model): Product = models.CharField(max_length=20, promary_key=True) Category = models.CharField(max_length=30) Rank = models.IntegerField() where the Rank is a rank within a Category . I\'d like to be able to iterate over all the Categories doing

Serializing Foreign Key objects in Django

只谈情不闲聊 提交于 2019-11-26 07:30:56
问题 I have been working on developing some RESTful Services in Django to be used with both Flash and Android apps. Developing the services interface has been quite simple, but I have been running into an issue with serializing objects that have foreign key and many to many relationships. I have a model like this: class Artifact( models.Model ): name = models.CharField( max_length = 255 ) year_of_origin = models.IntegerField( max_length = 4, blank = True, null = True ) object_type = models

Chaining multiple filter() in Django, is this a bug?

柔情痞子 提交于 2019-11-26 06:15:32
问题 I always assumed that chaining multiple filter() calls in Django was always the same as collecting them in a single call. # Equivalent Model.objects.filter(foo=1).filter(bar=2) Model.objects.filter(foo=1,bar=2) but I have run across a complicated queryset in my code where this is not the case class Inventory(models.Model): book = models.ForeignKey(Book) class Profile(models.Model): user = models.OneToOneField(auth.models.User) vacation = models.BooleanField() country = models.CharField(max

How to view corresponding SQL query of the Django ORM's queryset?

China☆狼群 提交于 2019-11-26 04:58:32
问题 Is there a way I can print the query the Django ORM is generating? Say I execute the following statement: Model.objects.filter(name=\'test\') How do I get to see the generated SQL query? 回答1: Each QuerySet object has a query attribute that you can log or print to stdout for debugging purposes. qs = Model.objects.filter(name='test') print qs.query Edit I've also used custom template tags (as outlined in this snippet) to inject the queries in the scope of a single request as HTML comments. 回答2:

Issue with ManyToMany Relationships not updating immediately after save

人走茶凉 提交于 2019-11-26 04:23:50
问题 I\'m having issues with ManytoMany Relationships that are not updating in a model when I save it (via the admin) and try to use the new value within a function attached to the post_save signal or within the save_model of the associated AdminModel . I\'ve tried to reload the object within those functions by using the get function with the id.. but it still has the old values. Is this a transaction issue? Is there a signal thrown when the transaction ends? Thanks, 回答1: When you save a model via

Optimizing database queries in Django REST framework

浪尽此生 提交于 2019-11-26 04:19:00
问题 I have the following models: class User(models.Model): name = models.Charfield() email = models.EmailField() class Friendship(models.Model): from_friend = models.ForeignKey(User) to_friend = models.ForeignKey(User) And those models are used in the following view and serializer: class GetAllUsers(generics.ListAPIView): authentication_classes = (SessionAuthentication, TokenAuthentication) permission_classes = (permissions.IsAuthenticated,) serializer_class = GetAllUsersSerializer model = User