django-orm

Migrating ManyToManyField to null true, blank true, isn't recognized

倖福魔咒の 提交于 2019-11-28 09:04:27
I have made a model change from standard = models.ManyToManyField(Standard) to standard = models.ManyToManyField(Standard, blank=True, null=True) South schemamigration for this app doesn't recognize the change? Similar to this question, which is unanswered: South migrations and changes to many-to-may fields That behavior is correct: null doesn't mean anything at the database level when used with a ManyToManyField . The declaration of a ManyToManyField causes the creation of an intermediate table to hold the relationship, and although Django will create a standard attribute on your model

Direct assignment to the forward side of a many-to-many set is prohibited. Use emails_for_help.set() instead

廉价感情. 提交于 2019-11-28 08:28:35
I am new to Django and didn't find any reference regarding this issue. I am getting this error when i use many to many field in django model(models.py). i guess the issue is assining m2m filed in view(views.py) from form(forms.py). How to assign m2m field in view. Django version 2.O python 3.5 models.py class User(AbstractUser): username=models.CharField(max_length=20) email = models.EmailField(_('email address'), unique=True) class Setupuser(models.Model): organization=models.CharField(max_length=200,blank=False,null=True) emails_for_help = models.ManyToManyField(User) views.py class Set_user

Django: select_related and GenericRelation

a 夏天 提交于 2019-11-28 07:03:44
Does select_related work for GenericRelation relations, or is there a reasonable alternative? At the moment Django's doing individual sql calls for each item in my queryset, and I'd like to avoid that using something like select_related. class Claim(models.Model): proof = generic.GenericRelation(Proof) class Proof(models.Model): content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey('content_type', 'object_id') I'm selecting a bunch of Claims, and I'd like the related Proofs to be pulled in instead of queried

Does Django queryset values_list return a list object?

匆匆过客 提交于 2019-11-28 07:00:12
问题 I have a Django app where users post photos, and other leave comments under the photos. When a comment is left, I need to notify: Everyone else who wrote in this thread The owner of the photo, in case they're not included in (1) For (1), I do: #I slice by 25 because I arbitrarily deem anyone beyond that irrelevant. all_commenter_ids = PhotoComment.objects.filter(which_photo=which_photo).order_by('-id').values_list('submitted_by', flat=True)[:25] Next, for (2), I try: all_relevant_ids = all

Subtracting two annotated columns

大城市里の小女人 提交于 2019-11-28 06:59:31
问题 I need to be able to sort on the aggregate of two annotated columns So I'd like to do something like this: c = c.annotate(metric=Sum('results__metric')) c = c.annotate(metric_prior=Sum('results__metric_prior')) c = c.annotate(variance=F('metric')-F('metric_prior')) #doesn't work, for demonstrative purposes only and then: c = c.order_by('variance') Does anyone know how to accomplish something like the above? 回答1: Actually, c = c.annotate(variance=F('metric')-F('metric_prior')) works as you

Accessing Many to Many “through” relation fields in Formsets

我与影子孤独终老i 提交于 2019-11-28 06:37:26
My Models: class End_User(models.Model): location = models.ForeignKey(Location) first_name = models.CharField(max_length=70, blank=True, null=True) email_address = models.CharField(max_length=70, blank=True, null=True) class Phone_Client(models.Model): end_user = models.ManyToManyField(End_User) ... extensions = models.CharField(max_length=20) class Line(models.Model): phone_client = models.ManyToManyField(Phone_Client, through='Phone_Line' ) .... voicemail = models.BooleanField(default=False) class Phone_Line(models.Model): phone_client = models.ForeignKey(Phone_Client) line = models

Testing postgres database accessibility from django

狂风中的少年 提交于 2019-11-28 05:29:46
问题 I am using the django ORM with a postgres database. A small group of users interact with it using import and export scripts. The database is only available on our intranet. If someone tries to use the database when postgres is unavailable the scripts hang. I would like to make the scripts test whether the database is available before attempting to process any data. I can connect to the database using the shell, import a model, and attempt to make a query: from myapp.models import mymodel

Django: List field in model?

爱⌒轻易说出口 提交于 2019-11-28 03:22:52
In my model, I want a field that has a list of triplets. e.g. [[1, 3, 4], [4, 2, 6], [8, 12, 3], [3, 3, 9]] . Is there a field that can store this data in the database? You can convert it into string by using JSON and store it as string. For example, In [3]: json.dumps([[1, 3, 4], [4, 2, 6], [8, 12, 3], [3, 3, 9]]) Out[3]: '[[1, 3, 4], [4, 2, 6], [8, 12, 3], [3, 3, 9]]' You can add a method into your class to convert it automatically for you. import json class Foobar(models.Model): foo = models.CharField(max_length=200) def set_foo(self, x): self.foo = json.dumps(x) def get_foo(self): return

How to work around lack of support for foreign keys across databases in Django

时光毁灭记忆、已成空白 提交于 2019-11-28 02:54:54
I know Django does not support foreign keys across multiple databases (originally Django 1.3 docs) But I'm looking for a workaround. What doesn't work I have two models each on a separate database. routers.py: class NewsRouter(object): def db_for_read(self, model, **hints): if model._meta.app_label == 'news_app': return 'news_db' return None def db_for_write(self, model, **hints): if model._meta.app_label == 'news_app': return 'news_db' return None def allow_relation(self, obj1, obj2, **hints): if obj1._meta.app_label == 'news_app' or obj2._meta.app_label == 'news_app': return True return None

Filtering on many-to-many relations that fulfill a set of criteria

时光毁灭记忆、已成空白 提交于 2019-11-28 02:17:37
With the following models: class OrderOperation(models.Model): ordered_articles = models.ManyToManyField(Article, through='orders.OrderedArticle') class OrderedArticle(models.Model): order_operation = models.ForeignKey(OrderOperation) article = models.ForeignKey(Article) articles = ... # some queryset containing multiple articles If I want to find order operations containing at least one article, this works as expected: OrderOperation.objects.filter(ordered_articles__in=articles) However, if I want to find order operations with all the articles in the order, what is the correct way to do it?