Django: Multiple Databases, Routers and Test Framework - django

I'm currently writing the tests for our django application.
Sadly we had to use a multiple database layout and can't change this. (Distributed databases with multiple backends on different servers across multiple datacenters)
We have two databases:
default databases with django default tables
application databases with some models
For these models we wrote different routers like documented on the django site.
Now the problem, if I run python manage.py test customerreceipts the test framework dies after some seconds with the following error:
django.db.utils.ProgrammingError: relation "auth_user" does not exist
I checked the created database and there were no tables. Because of this a query from a model throws the error.
The problem model is (in database 2):
class CustomerReceipts(models.Model):
def _choices_user():
users = User.objects.all()
users = users.order_by('id')
return [(e.id, e.username) for e in users]
# General
receipt_name = models.CharField(max_length=20, verbose_name="Receipt name") #: Receipt name
....
# Auditing
owner = models.IntegerField(verbose_name="Owner", choices=[('', '')] + _choices_user())
Because multiple database setup does not support direct links, we use an IntegerField for the owner and the business logic handles the integrity.
The problem is the _choices_user() which sets up an query for the missing table. What I don't understand is why django does not create the table auth_user in the first run. If I remove the app with the causing model, the test framework is working without any problem.
Any ideas how this can be fixed?
Thanks!
Edit:
I created a one database setup and tried the same thing. Sadly it throws the same error!
I'm confused now. Can someone test this too? Create a model with _choices_user method and run test.

You can manually select the database. Just call using(). Method using() takes a single argument: the alias of the database on which you want to run the query.
def _choices_user():
users = User.objects.using('default').all()
.....
.....
Django 1.7 docs

This is not exactly a perfect answer but the only way currently:
Model (removed choices):
class CustomerReceipts(models.Model):
# General
receipt_name = models.CharField(max_length=20, verbose_name="Receipt name") #: Receipt name
....
# Auditing
owner = models.IntegerField(verbose_name="Owner")
Admin:
class CustomerReceiptsAdminForm(forms.ModelForm):
class Meta:
model = CustomerReceipts
users = forms.ChoiceField()
def __init__(self, *args, **kwargs):
super(CustomerReceiptsAdminForm, self).__init__(*args, **kwargs)
owner = self.instance.owner
usersAll = User.objects.all()
usersAll = usersAll.order_by('id')
available_choices = [(e.id, e.username) for e in usersAll]
self.fields['users'].choices = available_choices
self.fields['users'].initial = owner
class CustomerReceipts(admin.ModelAdmin):
fields = ('abc', 'users')
list_display = ('abc', 'get_user')
form = CustomerReceiptsAdminForm
def save_model(self, request, obj, form, change):
obj.owner = form.cleaned_data['users']
obj.save()
def get_user(self, obj):
return User.objects.get(id=obj.owner).username
get_user.short_description = 'User'
This will handle all displaying in the admin view and selects the right customer upon editing.

Related

unique_together not working as expected in view

I added unique_together in the Django model. It works fine in the Django admin panel but doesn't work when I create model instances in a for loop in the Django view.
My Model:
class Parent(models.Model):
attribute_1 = models.CharField(max_length=100)
attribute_2 = models.IntegerField()
attribute_3 = models.CharField(max_length=100)
class Meta:
unique_together = (("attribute_1", "attribute_2"),)
View:
class ParentView(View):
template_name = "app/index.html"
def get(self, *args, **kwargs):
for i in range(10):
Parent.objects.create(
attribute_1=f"value-{i}",
attribute_2=i,
attribute_3=f"value-{i}",
)
return render(self.request, self.template_name)
Problem:
When I open this view, it creates the entries in the Parent model. when I refresh the page, I expect a unique value error but it doesn't give any error at all and creates entries again. No matter how much time I reload the page, it creates duplicate entries without giving an error.
What I tried?
I tried to change my DB from sqlite3 to PostgreSQL but it didn't work.
I tried to switch attributes but no luck.
It works perfectly fine on the admin panel.
It works perfectly fine on the admin panel.
That is because it is validated by a ModelForm, you can check this before saving with .full_clean(…) [Django-doc]:
class ParentView(View):
template_name = 'app/index.html'
def get(self, *args, **kwargs):
for i in range(10):
parent = Parent(
attribute_1=f'value-{i}',
attribute_2=i,
attribute_3=f'value-{i}',
)
parent.full_clean()
parent.save()
return render(self.request, self.template_name)
Most databases can enforce unique constraints, you will have to makemigrations and migrate.
Note: As the documentation on unique_together [Django-doc] says, the unique_together constraint will likely become deprecated. The documentation advises to use the UniqueConstraint [Django-doc] from Django's constraint
framework.

Is there a way to optimize the creation and retrieval of users (that are also part of another model) in Django with just one request?

My app has Users that can be Doctors/Patients/Secretaries. To create a Doctor, therefore, I perform two POST requests: one for the creation of a User and one for a Doctor. The way I do this, the User has to be created first so that I can later create the Doctor (Doctor requires a 'User' field). I am using Django Rest Framework to create the API.
class User(AbstractUser):
# defined roles so when I retrieve user, I know to perform a
# request to api/doctors/ or api/secretaries/ etc depending on role.
ROLES = (
('d', 'Doctor'),
('s', 'Secretary'),
('p', 'Patient'),
)
role = models.CharField(
max_length=1, choices=ROLES, blank=True, default='p', help_text='Role')
class Doctor(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
national_id = models.CharField(max_length=32, blank=False)
...
Since I'm new to Django, I don't know if two requests is the standard/best way of creating this User/Doctor.
This comes to mind as I am also thinking of the GET methods which will be performed later on (two GET requests when a Doctor logs in if I want to retrieve all of their info (User/Doctor)?)
I read about subclassing, which would be something like Doctor(User), then the only necessary request would be a single POST to create a Doctor (which would alongside create the User). I am, however, skeptical of subclassing the User as I read at least 3 SO answers stating it could cause problems in the future.
have a look at this good tutorial https://simpleisbetterthancomplex.com/tutorial/2018/01/18/how-to-implement-multiple-user-types-with-django.html which explain 2 different approches
extend AbstractUser with flags is_doctor, is_secretary, is_patient
class User(AbstractUser):
is_doctor = models.BooleanField('Doctor status', default=False)
is_secretary = models.BooleanField('Secretary status', default=False)
is_patient = models.BooleanField('Patient status', default=False)
using roles which suites your case:
class Role(models.Model):
'''
The Role entries are managed by the system,
automatically created via a Django data migration.
'''
ROLE_CHOICES = (
('d', 'Doctor'),
('s', 'Secretary'),
('p', 'Patient'),
)
id = models.PositiveSmallIntegerField(choices=ROLE_CHOICES, primary_key=True)
def __str__(self):
return self.get_id_display()
class User(AbstractUser):
roles = models.ManyToManyField(Role)
Problem
Determine best practices for handling multiple user types and adding attributes to a user in Django.
Solution
The following is are recommendations based on design patterns that have been commonly used in Django since version 0.96. These recommendations are present in Django’s documentation (see: References).
Roles
Use Django’s built in permissions module for role and group management instead of rolling your own role and group management.
Instead of a model for each type, create a single UserProfile model, relegating user types to being managed by permissions model.
I recommend using a data migration to add groups so that default groups are automatically seeded on initial migrate call—this reduces overhead for anyone setting up your project for the first time.
OneToOne
Use Django OneToOne field instead of ForeignKey.
UserProfile and Signals
Create a signal that creates a UserProfile on User create.
Example
class UserProfile(models.Model):
user = models.OneToOne(“User”, on_delete=models.CASCADE)
national_id = models.CharField(max_length=32,
def create_profile(sender, **kwargs):
user = kwargs["instance"]
if kwargs["created"]:
user_profile = UserProfile(user=user)
user_profile.save()
post_save.connect(create_profile, sender=User)
References
Django permissions (groups): https://docs.djangoproject.com/en/3.1/topics/auth/default/#groups
Django Data Migrations: https://docs.djangoproject.com/en/3.1/topics/migrations/#data-migrations
Django extending User model recommendation: https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#extending-django-s-default-user
Django post_save signal: https://docs.djangoproject.com/en/3.1/ref/signals/#post-save
in your case (the model you made) you can create the doctor and the user in one post request to the doctor creation by overriding the create function for the
from rest_framework.generics import CreateAPIView, ListAPIView
class CreateDoctorViewSet(CreateAPIView, ListAPIView):
def create(self, request, *args, **kwargs):
data = self.request.data
user_dict_keys = ["username", "email", "first_name", "last_name"]
user_dict = {key: data.pop(key, None) for key in user_dict_keys}
user_dict['role'] = "d"
user_serializer = UserSerializer(data=user_dict)
# if it's not valid it will return the exception details for the requester
user_serializer.is_valid(raise_exception=True)
user = user_serializer.create(user_dict)
user.set_password(data['password'])
data.pop("password", None)
user.save()
response = super().create(request, *args, **kwargs)
if response.statu_code == 201:
return response
# if an error happened while in the doctor model (model error or serializer error) >> delete the created user
user.delete()
return response
or made a little more DRY :
from rest_framework.generics import CreateAPIView, ListAPIView
def create_user(self):
data = self.request.data
user_dict_keys = ["username", "email", "first_name", "last_name"]
user_dict = {key: data.pop(key, None) for key in user_dict_keys}
user_dict['role'] = "d"
user_serializer = UserSerializer(data=user_dict)
# if it's not valid it will return the exception details for the requester
user_serializer.is_valid(raise_exception=True)
user = user_serializer.create(user_dict)
user.set_password(data['password'])
data.pop("password", None)
user.save()
return user
class CreateDoctorViewSet(CreateAPIView, ListAPIView):
def create(self, request, *args, **kwargs):
user = create_user(request)
response = super().create(request, *args, **kwargs)
if response.statu_code == 201:
return response
# if an error happened while in the doctor model (model error or serializer error) >> delete the created user
user.delete()
return response
Personal Advices:
in the case you're providing the User model has one role so it's better to make the user field in the Doctor class OneToOne instead of ForeignKey.
Of course if you have cases where there are people for example converting from Doctor to Secretary and you want them to switch between roles on the same account you can keep the ForeignKey on Doctor model but you have to make multiple roles possible in the user model.
What you mean by subclassing it is called Multi table inheritance. And there is no problem in using it, no side effects and it is perfectly compatible with Django Rest Framework (which you have tagged). This is the way it works:
class User(AbstractUser):
# Your common fields for all user types.
class Doctor(User):
national_id = models.CharField(max_length=32, blank=False)
class Secretary(User):
# Your specific fields for secretary model
class Patient(User):
# Your specifict fields for patient model
In background, it uses a OneToOne relationship for each subtype.
Advantajes of using Multi table inheritance:
It is simple and elegant: you don't have to take care of different tables, queries, etc; Django does it for you.
It also unsures a good and formalized structure of your database: different tables for common and specific data in OneToOne relationship.
It is suitable for your needs? That depends.
If each subtype has its own specific fields -> use multi table inheritance without doubt.
If each subtype has the same set of fields but different behaviours (different class/model methods, code, etc) -> use proxy models.
If all the subtypes have the same set of fields and the same behaviour (same class/model methods, code, etc) -> use role based approach (one field identifying the role).
(extra) If you have dozens of subtypes, each one of them has different fields and you don't care too much about database formalization -> Don't use Multi table inheritance. In this case, you can use a mix of role based approach with JSON fields (for storing all the specifict fields) and proxy models (for handling different behaviours)**.

django model - fetching user data accross multiple tables

I am writing a django (1.10) website and using allauth for authorisations. I don't want to extend the user model in django - because allauth adds a further layer of complexity to what is already a seemingly convoluted process.
I want to create a model (Custom UserManager?) that will have the following methods:
get_all_subscriptions_for_user(user=specified_user)
get_unexpired_subscriptions_for_user(user=specified_user)
Note: unexpired subscriptions are defined by subscriptions whose end_date > today's date.
This is a snippet of my models.py below
from django.db import models
from django.contrib.auth.models import User
#...
class Subscription(models.Model):
token = models.CharKey()
start_date = models.DateTime()
end_date = models.DateTime()
# other attributes
class UserSubscription(models.Model):
user = models.ForeignKey(User)
subscription = models.ForeignKey(Subscription)
# In view
def foo(request):
user = User.objects.get(username=request.user)
# how can I implement the following methods:
# get_all_subscriptions_for_user(user=specified_user)
# get_unexpired_subscriptions_for_user(user=specified_user)
Ideally, I would like to have a custom user manager, which can fetch this data in one trip to the database - but I'm not sure if I can have a custom user manager without having a custom user model.
[[Aside]]
I'm trying to avoid using a custom model, because it wreaks havoc on the other applications (in my project) which have User as a FK. makemigrations and migrate always barf with a message about inconsistent migration history
You can go with a custom Manager, don't need a UserManager since you are fetching related models:
class UserSubscriptionManager(models.Manager):
def for_user(self, user):
return super(UserSubscriptionManager, self).get_queryset().filter(user=user)
def unexpired_for(self, user):
return self.for_user(user).filter(
suscription__end_date__gt=datetime.date.today() # import datetime
)
in your models:
class UserSubscription(models.Model):
user = models.ForeignKey(User)
subscription = models.ForeignKey(Subscription)
user_objects = UserSubscriptionManager()
this way you can do chain filters in the view, for example:
unexpired_suscriptions = UserSubscription.user_objects().unexpired_for(
user=request.user
).exclude(suscription__token='invalid token')
Try this:
response = []
user_sub = UserSubscription.objects.filter(user=user.pk)
for row in user_sub:
subscription = Subscription.objects.get(pk=row.subscription)
end_date = subscription.end_date
if end_date > timezone.now():
response.append(subscription)

could I check through my view state of checkbox from db

My problem is next: I have device table with some params, one of them is device_able, if it is 'enable' I will do something with it. I added two actions to admin actions that change state of device_able on True or False and it works, but when I open any device from table my checkbox is always checked even if it False. I understood that I don't check data from db about state of checkbox but how to do it? I must use template but I do not understand how to connect my template that checks state of checkbox from db to my admin view of Dev app. Could you give me some useful links for exploring? Or I could check state in my admin.py file?
in my models.py
class Dev(models.Model):
#some params for device
device_able = models.BooleanField(default=False, choices=((True, 'enable'), (False, 'disable')))
def __unicode__(self):
return self.device_model
in admin.py
class DevAdminForm(forms.ModelForm):
class Meta:
widgets = {
'device_able': forms.CheckboxInput
}
full code of my admin.py
from django.contrib import admin
from dev.models import Dev
from django import forms
def make_enable(self, request, queryset):
queryset.update(device_able=True)
make_enable.short_description = "Mark selected devices as enable"
def make_disable(self, request, queryset):
queryset.update(device_able=False)
make_disable.short_description = "Mark selected devices as disable"
class DevAdminForm(forms.ModelForm):
class Meta:
widgets = {
'device_able': forms.CheckboxInput
}
class DevAdmin(admin.ModelAdmin):
fields = ['device_model', 'resolution', 'assets_format', 'scale_factor', 'device_able']
list_display = ('device_model', 'resolution', 'assets_format', 'scale_factor', 'device_able')
search_fields = ['device_model']
actions = [make_enable, make_disable]
form = DevAdminForm
class DevInline(admin.SimpleListFilter):
model = Dev
admin.site.register(Dev, DevAdmin)
If you're on the admin view you should use the AdminModel class from the admin module to have your model bound to the form. If you use a ModelForm you must supply the model it is bound to.
That said, in your cse the only need to subclass the AdminModle for your model is to insert tha admin actions, BooleanField are represented by CheckboxInput by default.
I would try in your admin.py:
class DevAdmin(admin.AdminModel):
def make_device_able()
...
actions = [make_device_able]
And register the the class with:
admin.site.register(Dev, DevAdmin)
Hope it helps.
I've found a solution for my problem. First of all, django is so cool that does all work for you. When I created my model I've set 'device_able' as models.CharField. It created in db field with type varchar. After that I've changed in my model 'device_able' to models.BooleanField and changed directly in db type of 'device_able' field on 'bool'. But my checkbox was always checked because only empty string returns False. when I created new project with with the same code, I mean device_able = models.BooleanField, my checkbox start to work correctly!

Django admin handling one to many relation

I have a django model as following
class Project(models.Model)
name=models.CharField(max_length=200)
class Application(models.Model)
proj=models.ForeignKey(Project, null=True, blank=True)
I need to modify the admin form of the project to be able to assign multiple applications to the project, so in the admin.py I have created a ModelAdmin class for the project as following
class ProjectAdmin(ModelAdmin)
form=projectForm
project_apps=[]
and the project form as following
class ProjectForm(forms.ModelForm):
class Meta:
model = Project
project_apps =forms.ModelMultipleChoiceField(queryset=Application.objects.all(),required=False,)
def __init__(self, *args, **kwargs):
super(ProjectForm, self).__init__(*args, **kwargs)
if self.instance.id is not None:
selected_items = [ values[0] for values in Application.objects.filter(project=self.instance) ]
self.fields['project_apps'].initial = selected_items
def save(self,commit=True):
super(ProjectForm,self).save(commit)
return self.instance
by doing this I have a multiple select in the create/edit project form.
what I need is to override the save method to save a reference for the project in the selected applications?
how can I get the selected applications ????
Not entirely sure what you're trying to do, but maybe this?
def save(self,commit=True):
kwargs.pop('commit') # We're overriding this with commit = False
super(ProjectForm,self).save(commit)
if self.instance:
for a in self.cleaned_data['project_apps']:
a.proj = self.instance
a.save()
return self.instance
Now, I can't remember if in this case, self.cleaned_data['project_apps'] will actually contain a list of Application objects or not. I suspect it will, but if not this function will take care of that:
def clean_project_apps(self):
app_list = self.cleaned_data['project_apps']
result = []
for a in app_list:
try:
result.append(Application.objects.get(pk=a)
except Application.DoesNotExist:
raise forms.ValidationError("Invalid application record") # to be safe
return result
All in all I think this form is a bad idea though, because basically what is happening here is you're displaying all of the application records which doesn't make sense, since most of them will be associated with other projects.
Oh oh oh!!! Just noticed you wanted this to show up in a Multiple Select list!
You're (probably) doing it wrong
A multiple select means this isn't a one-to-many relationship. It's a many-to-many relationship.
This is what you want to do, easy peasy, doesn't require any custom forms or anything.
class Project(models.Model)
name=models.CharField(max_length=200)
project_apps = models.ManyToMany('Application', null=True, blank=True)
class Application(models.Model)
# nothing here (NO foreign key, you want more than one App/Proj and vice versa)
Indicating that this is a many-to-many field in Project will automagically create the multiple select box in admin. Ta da!