django-orm

Model.objects.create equivalent that performs a full_clean?

安稳与你 提交于 2019-12-23 17:51:04
问题 So I've got the following code: class MyModel(models.Model): # Text language. ENGLISH = 'eng' FRENCH = 'fr' LANGUAGES_CHOICES = [ (ENGLISH, 'English'), (FRENCH, 'French'), ] language = models.CharField( max_length=max(len(language) for language in LANGUAGES_CHOICES), choices=LANGUAGES_CHOICES, blank=False, null=True) When I do MyModel(language='invalid').save() or MyModel.objects.create(language='invalid') , the model is saved without complaint. Is there any convenience method that is

Does Django's singleton architecture make it unworkable as a standalone ORM in a library?

岁酱吖の 提交于 2019-12-23 17:34:56
问题 I like the Django ORM. It's simple, easy to use, and reasonably powerful. I'm currently developing some internal sites for the VFX company I work for, for which I've used Django. In the meantime, we are developing other python applications and libraries to be used in various contexts in production. There's a number of places in which our core library needs to be interacting with some databases, and using an ORM like Django would really help things. I'm aware of other options like SqlAlchemy

How to tell if your select query is within a transaction or not?

∥☆過路亽.° 提交于 2019-12-23 15:44:13
问题 In Django 1.5.x, I have a long running management command where select queries are returning stale data. I suspect this is due to the fact that they are running within a transaction that are started earlier on the db connnection. Is there a way a tell if a query runs within a transaction or it is in autocommit mode? (this is somewhat more focused version of an earlier question I posted at https://stackoverflow.com/questions/18540099/orm-does-not-return-recent-database-changes) 回答1: You can

django.db.utils.IntegrityError: (1062, “Duplicate entry '22-add_' for key 'content_type_id'”)

不问归期 提交于 2019-12-23 03:22:17
问题 I am using django multiple DB router concepts, having multiple sites with different db's. Base database user will login with all other sub sites. When i try syncdb in base site its worked properly(at any time), but trying syncdb with other sites works first time only, if we try next time on-wards it throws integiry error like below django.db.utils.IntegrityError: (1062, "Duplicate entry '22-add_somesame' for key 'content_type_id'") Once i removed multiple DB router settings in that project

Django ORM how to reference to another row in query?

谁说我不能喝 提交于 2019-12-23 02:49:19
问题 I have the following table 'book': id shelf position (on shelf) 1 2 3 2 1 1 3 1 2 4 2 2 5 2 1 I need to move book4 to the end of shelf 1, therefore I need to set book4 position to [max(position) where shelf=1] + 1 and position where position=[max(position) where shelf=2] to book4 initial position (to close the gap) I tried this, but Max doesn't seem to work this way... Book.objects.filter(shelf=2).annotate(max_position_on_shelf=Max('position'))\ .filter(position=F('max_position_on_shelf'))\

Storage of DriveAPI credentials using oauth2client.django_orm.Storage-class

 ̄綄美尐妖づ 提交于 2019-12-23 02:44:21
问题 I want to save the client credentials obtained from Drive API. I tried the following code below, it's working well to store credentials but when I'm accessing the data in upload view it's not returning the credential views.py from oauth2client.django_orm import Storage from drv_app.models import CredentialsModel #authorization of the client by the user def authorize_application(request): #setting flow to get permission and code flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE,

How to select many to one to many without hundreds of queries using Django ORM?

*爱你&永不变心* 提交于 2019-12-22 18:30:46
问题 My database has the following schema: class Product(models.Model): pass class Tag(models.Model): product = models.ForeignKey(Product) attr1 = models.CharField() attr2 = models.CharField() attr3 = models.CharField() class AlternatePartNumber(models.Model): product = models.ForeignKey(Product) In other words, a Product has many Tag s, and a Product has many AlternatePartNumber s. Tag s are a collection of attributes of the Product . Given the three attributes in a Tag , I want to select the

How to select many to one to many without hundreds of queries using Django ORM?

Deadly 提交于 2019-12-22 18:30:08
问题 My database has the following schema: class Product(models.Model): pass class Tag(models.Model): product = models.ForeignKey(Product) attr1 = models.CharField() attr2 = models.CharField() attr3 = models.CharField() class AlternatePartNumber(models.Model): product = models.ForeignKey(Product) In other words, a Product has many Tag s, and a Product has many AlternatePartNumber s. Tag s are a collection of attributes of the Product . Given the three attributes in a Tag , I want to select the

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

落爺英雄遲暮 提交于 2019-12-22 14:53:25
问题 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 =

Django asymettrical self through relationship query

你离开我真会死。 提交于 2019-12-22 12:39:11
问题 I have the following - simplified - models: class User(models.Model): following = models.ManyToManyField("self", through='Following', symmetrical=False) class Following(models.Model): from_user = models.ForeignKey(User, related_name='from_user') to_user = models.ForeignKey(User, related_name='to_user') status = models.IntegerField() The status is 0 for pending, 1 for following Let user be a User. I would like to get all the followed users of user I can do user.following.all() to get all the