django-models

Unable to save with save_model using database router

送分小仙女□ 提交于 2020-01-03 13:32:31
问题 I am using a database router hence i have two databases for my application. One database for default django data and the other one. In my admin i have override the save_model function in order to save the created_by variable, but i am unable to do this. Cannot assign "<User: testuser>": the current database router prevents this relation. database router: from django.conf import settings class DatabaseAppsRouter(object): def db_for_read(self, model, **hints): """Point all read operations to

Django cascade save?

杀马特。学长 韩版系。学妹 提交于 2020-01-03 10:30:06
问题 I have a method on my user registration form that looks like this: def save(self): user = User( username = self.cleaned_data['username'], email = self.cleaned_data['email1'], first_name = self.cleaned_data['first_name'], last_name = self.cleaned_data['last_name'], ) user.set_password(self.cleaned_data['password1']) user.profile = Profile( primary_phone = self.cleaned_data['phone'], ) user.profile.address = Address( country = self.cleaned_data['country'], province = self.cleaned_data['province

Django: Perform case-insensitive lookups by default

不羁岁月 提交于 2020-01-03 09:23:20
问题 I need to perform case-insensitive queries on username by default when using the Django Auth framework. I tried fixing the issue by writing a custom subclass of Queryset and overriding the _filter_or_exclude method and then using that subclass in a custom manager for the User model- from django.db.models import Manager from django.db.models.query import QuerySet from django.contrib.auth.models import UserManager class MyQuerySet(QuerySet): def _filter_or_exclude(self, negate, *args, **kwargs)

Passing Instance to Django formset

瘦欲@ 提交于 2020-01-03 08:22:49
问题 How to pass a instance to the Django formset, The Scenario is like this. I have updated multiple rows by using a formset and, in a later stage i need to edit those values which i added earlier.(Editing) q = PaymentLines.objects.filter(pay_lines=project) formset = PayFormSet(prefix='payment', instance=q) 回答1: AuthorFormSet(queryset=Author.objects.all()) https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#changing-the-queryset 回答2: I have used it as follows the formset used is

Passing Instance to Django formset

一个人想着一个人 提交于 2020-01-03 08:22:13
问题 How to pass a instance to the Django formset, The Scenario is like this. I have updated multiple rows by using a formset and, in a later stage i need to edit those values which i added earlier.(Editing) q = PaymentLines.objects.filter(pay_lines=project) formset = PayFormSet(prefix='payment', instance=q) 回答1: AuthorFormSet(queryset=Author.objects.all()) https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#changing-the-queryset 回答2: I have used it as follows the formset used is

Django Model Choices: IntegerField vs CharField

寵の児 提交于 2020-01-03 07:17:54
问题 TL;DR : I have a table with millions of instances and I'm wondering how should I index it. I have a Django project that uses SQL Server as the database backend. After having a model with around 14 million instances in the Production environment, I realized that I was getting performance issues: class UserEvent(models.Model) A_EVENT = 'A' B_EVENT = 'B' types = ( (A_EVENT, 'Event A'), (B_EVENT, 'Event B') ) event_type = models.CharField(max_length=1, choices=types) contract = models.ForeignKey

django: how to change values for nullbooleanfield in a modelform?

六眼飞鱼酱① 提交于 2020-01-03 07:00:09
问题 In the docs, the nullbooleanfield is represented as a <select> box with "Unknown", "Yes" and "No" choices. How can I change the values of select to some other more meaningful texts and map it back to the yes,no and unknown values in my modelform ? For example I have yes_no_required = models.NullBooleanField() and I would like to have 'yes I acknowledge this' , 'no, I do not like this' and 'I do not know now' mapping to yes, no and required accordingly. 回答1: I spent half an hour putting

How to optimize a sort based on “latest” related model

天大地大妈咪最大 提交于 2020-01-03 06:05:14
问题 So say we have two models class Product(models.Model): """ A model representing a product in a website. Has new datapoints referencing this as a foreign key daily """ name = models.CharField(null=False, max_length=1024, default="To be Scraped") url = models.URLField(null=False, blank=False, max_length=10000) class DataPoint(models.Model): """ A model representing a datapoint in a Product's timeline. A new one is created for every product daily """ product = models.ForeignKey(Product, null

Django. Restrict each user to only vote once

佐手、 提交于 2020-01-03 05:44:09
问题 I have a situation and am happy with either one of two solutions, depending on which is more feasible/possible. I have a page that displays an event. That event's name may not necessarily be correct, and so users have the option to suggest corrections. Those corrections are stored in their own table with an Foreign Key relationship to the event. Once a suggestion has been made, users can vote up or down on the suggestion. I need to restrict each logged in user's maximum vote to 1. I don't

Django - Custom virtual model field with companion stored field [closed]

你。 提交于 2020-01-03 05:12:08
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 10 years ago . I would like to have a ConfirmationField field type. I want this field to work like a boolean field. I don't need to store this information on database instead I would like to store the date of confirmation on a seperate field. class MyModel(models.Model): confirmation = ConfirmationField() m = MyModel() m