I need two type of profile:
1) the first linked to Users by onetoone relationship to expand it with some new fields.
2) the seconds indipendent from Users (and accessible only to superuser/admin): they are just objects.
The problem is that I want a easy way to change a profile from 1°type to second type and viceversa.
In "Django Designer Patterns " I find an example like this:
class BaseProfile(models.Model):
User_Types = (
(0, 'active'),
(1, 'inactive'),
)
user_type = models.IntegerField(null=True, choices=User_Types, default=0)
name = models.CharField(max_length=32, unique=True)
#many other common field
class Meta:
abstract = True
def __str__(self):
return self.name
class Profile_Active(models.Model):
user = models.OneToOneField(User, blank=True, on_delete=models.PROTECT,
related_name='profile_active', primary_key=True) #this was in BaseProfile
#some more field reserved for this type of profile
class Meta:
abstract = True
class Profile_Inactive(models.Model):
#some field reserved for this type of profile
class Meta:
abstract = True
class Profile(Profile_Inactive, Profile_Active, BaseProfile):
pass
but I don't understand how I should combine the abstract models in Profile (what I should write instead of "pass"?) and what I should achieve? Profile
will have all the fields of the three models? Which the gain respect to use a single model?
Maybe I can forget Profile and use Profile_Inactive(BaseProfile) and Profile_Active(BaseProfile)? but then how to change from one to the other?
I'm confused, help please
Related
class Patient(models.Model):
user = models.OneToOneField(User, related_name='patient', on_delete=models.CASCADE)
id_type = models.CharField(max_length=300)
id_number = models.CharField(max_length=300)
creation_date = models.DateField(default=datetime.date.today)
class Allergie(models.Model):
name = models.CharField(max_length=300, default="X")
class PatientAllergies(models.Model):
patient = models.ForeignKey(Patient, related_name="patient_allergies", on_delete=models.CASCADE)
allergie = models.ForeignKey(Allergie, on_delete=models.CASCADE, null=True)
professional_contract = models.ForeignKey(ProfessionalContract, null=True ,on_delete=models.CASCADE)
Is it possible to retrieve a patient objecto with a property that is a list of all his allergies, including name and id with these models?
you have the PatientAllergies as a chain,
so
patientAllergies = PatientAllergies.objects.get(patient.id_number='0000')
patientAllergies.allergie #you get the single allergie model connect with it, take care it is a foreignKey so it is singolar and not many
patientAlleriges.patient.user #will give you access to all the data of the user
You can achieve this with prefetch_related and Prefetch like so:
Patient.objects.prefetch_related(
Prefetch('patient_allergies__allergie', to_attr='allergies')
)
EDIT: Just learned that to_attr will not work on multiple levels of prefetch. Another approach I can think of is use a model property for Patient that returns its related allergies like this:
class Patient(models.Model):
#property
def allergies(self):
return Allergie.objects.filter(patientallergies_set__patient=self)
Then in your serializer, the allergies field can use the Allergies serializer
I have Three model defined. I want to filter Get specific user and get all related fields in the result set. For example, I want to get supplierInfo and company info of that user.
class User(AbstractBaseUser):
user_email= models.EmailField(unique=True, max_length=254)
user_id = models.AutoField(primary_key=True)
staff=models.BooleanField(default=True)
admin=models.BooleanField(default=True)
role_id=models.IntegerField(default=0)
supplier=models.ForeignKey(Supplier, on_delete=models.CASCADE) ....
class Company(models.Model):
company_name= models.CharField(('company name'), max_length=255, blank=True)
company_id = models.AutoField(primary_key=True)
class Meta:
verbose_name = ('company')
verbose_name_plural = ('company')
db_table = "company"
def __str__(self):
return self.company_name
class Supplier(models.Model):
supplier_name= models.CharField(('supplier name'), max_length=255, blank=True)
supplier_id = models.AutoField(primary_key=True)
company=models.ForeignKey(Company, on_delete=models.CASCADE)
class Meta:
verbose_name = ('supplier')
verbose_name_plural = ('supplier')
db_table = "supplier"
def __str__(self):
return self.supplier_name
I Have tried this
userInfo=User.objects.filter(user_id__exact=user.user_id).get()
userRelated= Supplier.objects.filter(supplier_id__exact=userInfo.supplier_id).get()
companyRelated=Company.objects.filter(company_id__exact=userRelated.company_id).get()
I am getting expected result But I dont think it is the best way. I want to merge three queries and get the result in one object . I am new in django so please help me out.
You can create a one to one relationship by defining a foreign key.
https://docs.djangoproject.com/en/3.0/topics/db/examples/one_to_one/
The documentation is really well written and i suggest you take a look for yourself.
If you do this you will have access to the related table when using the typical django orm methods.
I am creating an abstract model for my django app, SrdObject. One of the characteristics of my model is that it has a pair of fields that, taken together, must be unique: 'name' and the foreign key 'module'.
Here is an example of what I had
class SrdObject(models.Model):
name = models.CharField(max_length=50)
slug_name = models.SlugField(max_length=75, unique=True)
active = models.BooleanField(default=True)
module = models.ForeignKey(Module, on_delete=models.CASCADE, related_name='%(class)s', blank=False, null=False, default='default')
class Meta:
unique_together = ['name', 'module']
ordering = ['name']
abstract = True
This seemed to be working ok, but the unique_together attribute has been marked as deprecated by django (See here), so I changed it to this
class Meta:
constraints = [
models.UniqueConstraint(fields=['name', 'module'], name='unique-in-module')
]
ordering = ['name']
abstract = True
This doesn't work because the name field must be unique, and since this is an abstract class, the constraint is repeated over several models.
I also tried
models.UniqueConstraint(fields=['name', 'module'], name='{}-unique-in-module'.format(model_name))
But obviously this falls into scoping problems, so I tried a decorator method
def add_unique_in_module_constraint(cls):
cls._meta.constraints = [
models.UniqueConstraint(fields=['name', 'module'], name='unique-in-module')
]
return cls
#add_unique_in_module_constraint
class SrdObject(models.Model):
class Meta:
ordering = ['name']
abstract = True
But this didn't seem to do anything.
So how do I create a models.UniqueConstraint in abstract model if every constraint needs a unique name attribute?
LATEST UPDATE
Since 3rd version you finally can do that by specifying interpolation:
Changed in Django 3.0:
Interpolation of '%(app_label)s' and '%(class)s' was added.
Example:
UniqueConstraint(fields=['room', 'date'], name='%(app_label)s_unique_booking')
OLD (Django < 3.0)
You can't do that, same problem for me, so sad...
Source: django docs
Constraints in abstract base classes
You must always specify a unique name for the constraint. As such, you cannot normally specify a constraint on an abstract base class, since the Meta.constraints option is inherited by subclasses, with exactly the same values for the attributes (including name) each time. Instead, specify the constraints option on subclasses directly, providing a unique name for each constraint.
I took this model setup:
class Module(models.Model):
pass
class SrdObject(models.Model):
name = models.CharField(max_length=50)
slug_name = models.SlugField(max_length=75, unique=True)
active = models.BooleanField(default=True)
module = models.ForeignKey(Module, on_delete=models.CASCADE, related_name='%(class)s', blank=False, null=False, default='default')
class Meta:
constraints = [
models.UniqueConstraint(fields=['name', 'module'], name='unique-in-module')
]
ordering = ['name']
abstract = True
class SrdObjectA(SrdObject):
pass
class SrdObjectB(SrdObject):
pass
And then ran these tests:
class TestSrdObject(TestCase):
#classmethod
def setUpTestData(cls):
cls.module = Module.objects.create()
SrdObjectA.objects.create(name='A', module=cls.module)
def test_unique_applies_to_same_model(self):
with self.assertRaises(IntegrityError):
SrdObjectA.objects.create(name='A', module=self.module)
def test_unique_does_not_apply_to_different_model(self):
self.assertTrue(SrdObjectB.objects.create(name='A', module=self.module))
And they pass. Perhaps I'm still missing the problem you're running into?
I am learning Django, and I've found myself in the following scenario: I have a project model with various fields. One of which is key_personnel. The other is key_person_roles, which defines the roles played by each key personnel on the project.
A project can have various key personnel, and each key personnel can have various roles.
Key personnel do not have default roles, they can play any roles on different projects.
How can I best model this scenario in Django? I can't seem to find examples on how to model such a scenario. Here's my models.py:
from django.db import models
from phonenumber_field.modelfield import PhoneNumberField
class Key_Person(models.Model):
name = models.CharField(max_length=200)
designation = models.CharField(max_length=100)
email = models.EmailField()
contact = PhoneNumberField()
class Meta:
ordering = ['name']
verbose_name_plural = "Key Persons"
def __unicode__(self):
return self.name
class Key_Person_Role(models.Model):
role = models.CharField(max_length=100)
class Meta:
verbose_name_plural = 'Key Person Roles'
def __unicode__(self):
return self.role
class Project(models.Model):
title = models.CharField(max=150)
description = models.TextField()
# more fields here ...
key_persons = models.ManyToManyField(Key_Person)
# What I want is to assign a role to each key person above.
key_person_roles = models.ManyToManyField(Key_Person_Role)
def __unicode__(self):
return self.title
I appreciate your help.
I'd use a custom through model/table for the m2m relation between Person and Project (*refusing to use your mix of camel and snake case). This model can then have a ForeignKey to a Role. This way, every relation between a person and project is characterized by a (reusable!) role:
class Person(models.Model):
....
# model to capture the different types of roles
class Role(models.Model):
name = models.CharField(max_length=100)
class Project(models.Model):
# specify custom through model
key_persons = models.ManyToManyField('Person', through='ProjectPerson')
# m2m-model for project-person relation
class ProjectPerson(models.Model):
person = models.ForeignKey('Person', ...)
project = models.ForeignKey('Project', ...)
# such a relation has a role!
role = models.ForeignKey('Role', ...)
class Meta:
unique_together = (('person', 'project'),)
I have run into a problem developing my Django site.
from django.db import models
class TitlePost(models.Model):
title_name = models.CharField(max_length=100, unique=True)
title_body = models.TextField(max_length=30000)
title_why = models.TextField(max_length=250, null=True)
title_publication_date = models.DateTimeField('date')
likes = models.IntegerField(default=0)
dislikes = models.IntegerField(default=0)
def __unicode__(self):
return self.title_name
class TopTitlesPostPage(models.Model):
title_post = models.OneToOneField(TitlePost)
hello = models.CharField(max_length=100, unique=True)
def __unicode__(self):
return self.hello
class NewTitlesPostPage(models.Model):
title_post = models.OneToOneField(TitlePost)
hello = models.CharField(max_length=100, unique=True)
def __unicode__(self):
return self.hello
Why don't TopTitlesPostPage and NewTitlesPostPage inherit all the attributes from TitlePost? For instance, if I try to call the likes in my template using TopTitlesPostPage, it will not execute because the likes attribute is not inherited. Does OneToOneField have something to do with the problem? I did read that making TitlePost a meta class will help but I need it to have a table in my database. I actually want all of them to have a table in my data base. Then again, maybe I am approaching this the wrong way and I should use just TitlePost as a model to generate everything?
The behaviour you would like to see is called multi table inheritance. Every child class internally ends up with the same thing that you wrote, so with a one to one field to the base class TitlePost, but it's internally managed by django.
If you do multiple inheritance like the code below you will be able to write:
k=TopTitlesPostPage.objects.create(hello="Hello",title_name="Heh")
That means the fields will be directly accessible.
from django.db import models
class TitlePost(models.Model):
title_name = models.CharField(max_length=100, unique=True)
title_body = models.TextField(max_length=30000)
title_why = models.TextField(max_length=250, null=True)
title_publication_date = models.DateTimeField('date')
likes = models.IntegerField(default=0)
dislikes = models.IntegerField(default=0)
def __unicode__(self):
return self.title_name
class TopTitlesPostPage(TitlePost):
hello = models.CharField(max_length=100, unique=True)
def __unicode__(self):
return self.hello
class NewTitlesPostPage(TitlePost):
hello = models.CharField(max_length=100, unique=True)
def __unicode__(self):
return self.hello
In case you are never actually going to reference the base class TitlePost, but only its children it might be more appropriate to make `TitlePost abstract:
class TitlePost(models.Model):
title_name = models.CharField(max_length=100, unique=True)
title_body = models.TextField(max_length=30000)
title_why = models.TextField(max_length=250, null=True)
title_publication_date = models.DateTimeField('date')
likes = models.IntegerField(default=0)
dislikes = models.IntegerField(default=0)
class Meta:
abstract = True
def __unicode__(self):
return self.title_name
Making TitlePostabstract will omit the creation of the table TitlePostin the database, and the child models will end up with the fields of the base class inserted into their own tables seperately. If the base class is just for factoring out common functionality this is the preferred way to go.
For huge queries this will also make a difference in performance because the ORM will need to do less JOINoperations.
It's not possible to install Foreign Keys to abstract models in Django.
You can however install Foreign Keys to a non abstract base class. The only limitation is that the reverse Foreign Key relation will return the base class instances.
You can circumvent this limitation by using django-polymorphic.
Django Polymorphic allows you to query the base class objects but retrieves the child class instances:
>>> Project.objects.create(topic="Department Party")
>>> ArtProject.objects.create(topic="Painting with Tim", artist="T. Turner")
>>> ResearchProject.objects.create(topic="Swallow Aerodynamics", supervisor="Dr. Winter")
>>> Project.objects.all()
[ <Project: id 1, topic "Department Party">,
<ArtProject: id 2, topic "Painting with Tim", artist "T. Turner">,
<ResearchProject: id 3, topic "Swallow Aerodynamics", supervisor "Dr. Winter"> ]
To use django polymorphic you only need to declare your models with Polymorphic Model as base class:
from django.db import models
from polymorphic import PolymorphicModel
class ModelA(PolymorphicModel):
field1 = models.CharField(max_length=10)
class ModelB(ModelA):
field2 = models.CharField(max_length=10)
class ModelC(ModelB):
field3 = models.CharField(max_length=10)
Foreign keys will also return the child class instances, which is really cool if you're trying to be polymorphic.
# The model holding the relation may be any kind of model, polymorphic or not
class RelatingModel(models.Model):
many2many = models.ManyToManyField('ModelA') # ManyToMany relation to a polymorphic model
>>> o=RelatingModel.objects.create()
>>> o.many2many.add(ModelA.objects.get(id=1))
>>> o.many2many.add(ModelB.objects.get(id=2))
>>> o.many2many.add(ModelC.objects.get(id=3))
>>> o.many2many.all()
[ <ModelA: id 1, field1 (CharField)>,
<ModelB: id 2, field1 (CharField), field2 (CharField)>,
<ModelC: id 3, field1 (CharField), field2 (CharField), field3 (CharField)> ]
Take into account that these queries will be slightly less performant.
U need to extend the classes like follows:
class TopTitlesPostPage(TitlePost):
U can add more and inherit from multiple models just by mentionin g all the models comma separated! This all the fields from the models will be created in the child class as well
EDIT:
The way i would do it is to create an Abstract class which contains all your common fields and extend it into your TitlePost, TopTitlesPostPagea and NewTitlesPostPage
You need to have TopTitlesPostPage and NewTitlesPostPage extend the base class of TitlePost like so ...
class TopTitlesPostPage(models.Model)
You don't need a OneToOneField if you are inheriting from the base class, since the attributes of TitlePost will be available to you in the subclass. If you want to make TitlePost abstract (you can not declare an instance of that class, only inherit from it) you have to add it to the meta class
class TitlePost(models.Model):
class Meta:
abstract = True
Here is a link to the documentation.