Django Model not defined - python

I've been working on some views and using a model for like a week now and there was no issues until now: I did not change anything but add a new field to the model, and now myModel.objects.create() gives me a name is not defined error. I have the model imported and as I said, I've been working on this for a week and I created several models using the exact same code.
models:
class Prescription(models.Model):
prescription_id = models.CharField(max_length=26, default=prescription_id, editable=False, unique=True) # THIS IS THE FIELD I ADDED BEFORE HAVING THIS ERROR
appointment = models.ForeignKey('booking.Appointment', on_delete=models.SET_NULL, null=True, blank=True)
doctor = models.ForeignKey('core.Doctor', on_delete=models.CASCADE)
patient = models.ForeignKey('core.Patient', on_delete=models.CASCADE)
overriden_date = models.DateField(null=True, blank=True)
control = models.BooleanField(default=False)
control_date = models.DateField(null=True, blank=True)
send_email = models.BooleanField(default=False)
posted = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Meta:
verbose_name = "Ordonnance"
verbose_name_plural = "Ordonnances"
def __str__(self):
return str(self.id)
class PrescriptionElement(models.Model):
prescription = models.ForeignKey('Prescription', on_delete=models.CASCADE)
drug = models.ForeignKey(Drug, on_delete=models.CASCADE, null=True)
custom = models.CharField("Elément", max_length=25, null=True, blank=True)
dosage = models.CharField(max_length=45, null=True, blank=True)
posologie = models.CharField(max_length=256, null=True, blank=True)
duration = models.CharField("Durée", max_length=15, null=True, blank=True)
views:
PrescriptionElement.objects.create(prescription=prescription, drug=listemedicament, custom=medicament, dosage=dosage, posologie=posologie, duration=duree)
error:
name 'PrescriptionElement' is not defined

I fixed the issue by removing any circular relation between multiple files. Circular Imports are the one problem causing this. The prescription_id default value was defined in a file called utils.py, and in utils.py there are some functions inheritting models from the models.py file. Removing this circular relationship fixes this issue.
Also, using this format to defined ForeignKeys ('booking.Appointment') is the better choice to avoir importing.

Related

How to join tables with O2M and M2M relationship?

I have the following related data models
class Cart(models.Model):
products = models.ManyToManyField('assortment.Product', through='CartProduct')
order = models.OneToOneField('Order', on_delete=models.CASCADE, related_name='order_cart', null=True)
user = models.ForeignKey('account.Profile', on_delete=models.CASCADE, related_name='user_carts', blank=True, null=True)
class CartProduct(models.Model):
product = models.ForeignKey('assortment.Product', related_name='product_cartproducts', null=True, on_delete=models.SET_NULL)
cart = models.ForeignKey('Cart', related_name='cart_cartproducts', null=True, on_delete=models.SET_NULL)
count = models.IntegerField(blank=False, null=False, default=1)
class Order(models.Model):
pay_date = models.DateField(auto_now_add=True)
is_paid = models.BooleanField(default=False)
My code below gives an error: invalid argument "products" in prefetch_related:
Order.objects.all().select_related('order_cart').prefetch_related('products')
How can I join all three models in one request?
The query builder continues with the original table (Order), thus you have to specify the fields relative to that or relative to the previously mentioned field. Try one of the following:
'order_cart__products'
'order_cart__cart_cartproducts'
(Notice the double underscore.)

What is the best way to handle different but similar models hierarchy in Django?

What is the deal: I'm crating a site where different types of objects will be evaluated, like restaurants, beautysalons, car services (and much more).
At the beginning I start with one app with with Polymorfic Model:
models.py:
from django.db import models
from users.models import ProfileUser
from django.utils import timezone
from polymorphic.models import PolymorphicModel
class Object(PolymorphicModel):
author = models.ForeignKey(ProfileUser, on_delete=models.CASCADE)
title = models.CharField(max_length=300)
city = models.ForeignKey(City, on_delete=models.CASCADE)
address = models.CharField(max_length=300)
phone = models.CharField(max_length=20, default='')
email = models.CharField(max_length=100, default='')
site = models.CharField(max_length=100, default='')
facebook = models.CharField(max_length=100, default='')
instagram = models.CharField(max_length=100, default='')
content = models.TextField()
rating = models.DecimalField(default=10.0, max_digits=5, decimal_places=2)
created_date = models.DateTimeField(default=timezone.now)
approved_object = models.BooleanField(default=False)
admin_seen = models.BooleanField(default=False)
def __str__(self):
return f"{self.title}"
class Restaurant(Object):
seats = models.IntegerField()
bulgarian_kitchen = models.BooleanField(default=False)
italian_kitchen = models.BooleanField(default=False)
french_kitchen = models.BooleanField(default=False)
sea_food = models.BooleanField(default=False)
is_cash = models.BooleanField(default=False)
is_bank_card = models.BooleanField(default=False)
is_wi_fi = models.BooleanField(default=False)
category_en_name = models.CharField(max_length=100, default='restaurants')
category_bg_name = models.CharField(max_length=100, default='Ресторанти')
bg_name = models.CharField(max_length=100, default='Ресторант')
is_garden = models.BooleanField(default=False)
is_playground = models.BooleanField(default=False)
class SportFitness(Object):
is_fitness_trainer = models.BooleanField(default=False)
category_en_name = models.CharField(max_length=100, default='sportfitness')
category_bg_name = models.CharField(max_length=100, default='Спорт и фитнес')
bg_name = models.CharField(max_length=100, default='Спорт и фитнес')
class CarService(Object):
is_parts_clients = models.BooleanField(default=False)
category_en_name = models.CharField(max_length=100, default='carservice')
category_bg_name = models.CharField(max_length=100, default='Автосервизи')
bg_name = models.CharField(max_length=100, default='Автосервиз')
class Comment(models.Model):
object = models.ForeignKey(Object, on_delete=models.CASCADE, related_name='comments')
author = models.ForeignKey(ProfileUser, on_delete=models.CASCADE)
content = models.TextField()
rating = models.TextField()
approved_object = models.BooleanField(default=False)
admin_seen = models.BooleanField(default=False)
created_date = models.DateTimeField(default=timezone.now)
def __str__(self):
return f"{self.content}"
class Images(models.Model):
object = models.ForeignKey(Object, default=None, on_delete=models.CASCADE)
image = models.ImageField(upload_to='attachments',
verbose_name='Image')
class ObjectCoordinates(models.Model):
object = models.ForeignKey(Object, on_delete=models.CASCADE, related_name='coordinates')
latitude = models.CharField(max_length=60)
longitude = models.CharField(max_length=60)
Don't mention that name Object is wrong, I already know that :)
So all logic about different objects was in one App and this start to cause some problems, like:
views.py:
def show_object(request, category, pk, page_num):
categories = {'restaurants' : 'Restaurant', 'sportfitness' : 'SportFitness', 'carservice' : 'CarService'} # probably this is not good way to do it
obj = apps.get_model('objects', categories[category]).objects.get(id=pk)
def show_all_objects(request, category, page_num, city=None):
params_map = {
'restaurants': Restaurant,
'sportfitness': SportFitness,
'carservice': CarService,
}
objects = Object.objects.instance_of(params_map.get(category))
and other problems in templates (a lot of if-else blocks) etc.
So I decide to change whole structure and put every model in different app, so now I have app:restaurants, app:sportfitness, app:carservices, etc. But it begin to cause some problems, again, like this model:
class ObjectCoordinates(models.Model):
object = models.ForeignKey(Object, on_delete=models.CASCADE, related_name='coordinates')
latitude = models.CharField(max_length=60)
longitude = models.CharField(max_length=60)
All of objects (restaurants, car services) has coordinates of map, so I'm not sure how to handle it, with Model ObjectCoordinates . If I create ObjectCoordinates for each of them, respectively a table in BD (then I will have some tables with different names but same structure, which is not very good, because except ObjectCoordinates, models share and other common models like Images and others, so at the end I will have a lot of tables with different names and same structure). Probably I should add one more column for object category, if I got two rows with same id of objects?
Probably change ObjectCoordinates and other common models to ManyToMany relation will prevent identical tables, but I'm not quite sure about that. Other problem is that there is a lot of repeated code (in views, templates). Also, now, I don't know how to get all objects (restaurants, car services) when they do not have common point, like Object model in first scenario with Polymorphic Model. Or I should keep different apps but to create common Model for all objects, and all of them to to inherit it.
Questions:
What structure is better, first one or second one?
What is the best wayt to implement such site (model structure)?
Should I create common point (model) for all models who they will inherit?
Here is my third attempt (notice that Object is renamed to Venue):
from django.db import models
from users.models import ProfileUser
from django.utils import timezone
from polymorphic.models import PolymorphicModel
# Create your models here.
class City(models.Model):
name = models.CharField(max_length=20)
def __str__(self):
return f"{self.name}"
class Category(models.Model):
name = models.CharField(max_length=20)
bg_name = models.CharField(max_length=20, default=None)
category_bg_name = models.CharField(max_length=100, default=None)
def __str__(self):
return f"{self.name}"
class Venue(models.Model):
author = models.ForeignKey(ProfileUser, on_delete=models.CASCADE)
title = models.CharField(max_length=300)
city = models.ForeignKey(City, on_delete=models.CASCADE)
address = models.CharField(max_length=300)
phone = models.CharField(max_length=20, default='')
email = models.CharField(max_length=100, default='')
site = models.CharField(max_length=100, default='')
facebook = models.CharField(max_length=100, default='')
instagram = models.CharField(max_length=100, default='')
content = models.TextField()
rating = models.DecimalField(default=10.0, max_digits=5, decimal_places=2)
created_date = models.DateTimeField(default=timezone.now)
approved_venue = models.BooleanField(default=False)
admin_seen = models.BooleanField(default=False)
venue_category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='category')
def __str__(self):
return f"{self.title}"
class VenueFeatures:
seats = models.IntegerField()
bulgarian_kitchen = models.BooleanField(default=False)
italian_kitchen = models.BooleanField(default=False)
french_kitchen = models.BooleanField(default=False)
sea_food = models.BooleanField(default=False)
is_cash = models.BooleanField(default=False)
is_bank_card = models.BooleanField(default=False)
is_wi_fi = models.BooleanField(default=False)
is_garden = models.BooleanField(default=False)
is_playground = models.BooleanField(default=False)
is_fitness_trainer = models.BooleanField(default=False)
is_parts_clients = models.BooleanField(default=False)
is_hair_salon = models.BooleanField(default=False)
is_laser_epilation = models.BooleanField(default=False)
is_pizza = models.BooleanField(default=False)
is_duner = models.BooleanField(default=False)
is_seats = models.BooleanField(default=False)
is_external_cleaning = models.BooleanField(default=False)
is_internal_cleaning = models.BooleanField(default=False)
is_engine_cleaning = models.BooleanField(default=False)
is_working_weekend = models.BooleanField(default=False)
is_kids_suitable = models.BooleanField(default=False)
is_working_weekend = models.BooleanField(default=False)
venue = models.ForeignKey(Venue, on_delete=models.CASCADE, related_name='venue')
class Comment(models.Model):
venue = models.ForeignKey(Venue, on_delete=models.CASCADE, related_name='comments')
author = models.ForeignKey(ProfileUser, on_delete=models.CASCADE)
content = models.TextField()
rating = models.TextField()
approved_venue = models.BooleanField(default=False)
admin_seen = models.BooleanField(default=False)
created_date = models.DateTimeField(default=timezone.now)
def __str__(self):
return f"{self.content}"
class Images(models.Model):
venue = models.ForeignKey(Venue, default=None, on_delete=models.CASCADE)
image = models.ImageField(upload_to='attachments',
verbose_name='Image')
class VenueCoordinates(models.Model):
venue = models.ForeignKey(Venue, on_delete=models.CASCADE, related_name='coordinates')
latitude = models.CharField(max_length=60)
longitude = models.CharField(max_length=60)
Now I do not now how to use Venue with VenueFeatures
Notice that features are just true/false values (checkboxes in form).
Okay, this is probably the best way to abstract anything as much as I can:
from django.db import models
from users.models import ProfileUser
from django.utils import timezone
from polymorphic.models import PolymorphicModel
# Create your models here.
class City(models.Model):
name = models.CharField(max_length=20)
def __str__(self):
return f"{self.name}"
class Category(models.Model):
name = models.CharField(max_length=20)
bg_name = models.CharField(max_length=20, default=None)
category_bg_name = models.CharField(max_length=100, default=None)
icon = models.CharField(max_length=40, default=None)
def __str__(self):
return f"{self.name}"
class Venue(models.Model):
author = models.ForeignKey(ProfileUser, on_delete=models.CASCADE)
title = models.CharField(max_length=300)
city = models.ForeignKey(City, on_delete=models.CASCADE)
address = models.CharField(max_length=300)
phone = models.CharField(max_length=20, default='')
email = models.CharField(max_length=100, default='')
site = models.CharField(max_length=100, default='')
facebook = models.CharField(max_length=100, default='')
instagram = models.CharField(max_length=100, default='')
content = models.TextField()
rating = models.DecimalField(default=10.0, max_digits=5, decimal_places=2)
created_date = models.DateTimeField(default=timezone.now)
approved_venue = models.BooleanField(default=False)
admin_seen = models.BooleanField(default=False)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
def __str__(self):
return f"{self.title}"
class Feature(models.Model):
name = models.CharField(max_length=100)
code = models.CharField(max_length=100 )
category = models.ForeignKey(Category, on_delete=models.CASCADE)
type = models.CharField(max_length=100)
def __str__(self):
return f"{self.name}"
class VenueFeatures(models.Model): # ManyToMany Venues <-> Features
venue = models.ForeignKey(Venue, on_delete=models.CASCADE)
feature = models.ForeignKey(Feature, on_delete=models.CASCADE)
value = models.CharField(max_length=255)
class Comment(models.Model):
venue = models.ForeignKey(Venue, on_delete=models.CASCADE, related_name='comments')
author = models.ForeignKey(ProfileUser, on_delete=models.CASCADE)
content = models.TextField()
rating = models.TextField()
approved_venue = models.BooleanField(default=False)
admin_seen = models.BooleanField(default=False)
created_date = models.DateTimeField(default=timezone.now)
def __str__(self):
return f"{self.content}"
class Images(models.Model):
venue = models.ForeignKey(Venue, default=None, on_delete=models.CASCADE)
image = models.ImageField(upload_to='attachments',
verbose_name='Image')
class VenueCoordinates(models.Model):
venue = models.ForeignKey(Venue, on_delete=models.CASCADE, related_name='coordinates')
latitude = models.CharField(max_length=60)
longitude = models.CharField(max_length=60)
Now Features are bound with Categories
Also Venues are ManyToMany with Features
I have already linked it to business logic and it works fine.
TL;DR Use a JSONField (JSONB automatically I think) in PostgreSQL WITHOUT a GIN index for your VenueFeatures instead of creating an entirely new model. Postgres has come a long way towards NoSQL/unstructured DB and it's really good. Using a JSONField in your Venue model would work really well. At the very bottom, I talk about how I would design your site's db.
Although I hate saying this, but this could be the job of a NoSQL database. Usually every application uses RDBM which is structured, but you are using unstructured attributes. You could try using PostgreSQL's JSONB field but... stuffing everything into one field would be tiresome for the GIN index + caching.
For now, I'll ignore a lot of weird practices such as needing to partition a couple of attributes, max_length for char field is typically 255 length for all databases, making sure the most accessed tables don't have too many attributes so that caching is better (i.e. you don't have to invalidate your cache every time a user updates your table), GeoDjango for your coordinate system with the standard Mercator projection system on Postgres Geography mode, and you could use sets instead of dicts (sets are iterables and use {} but nothing is repeated)...
Stay away from this option: For one, I NEVER recommend MongoDB, but it could be useful for you... so long as your application doesn't grow too large as in a couple million records could break your system.
The other RECOMMENDED option is PostgreSQL's JSONB or Django's JSONField withOUT a GIN index (I strongly recommend you don't index this field since venues could change them sooo often to the point that REINDEXING and caching would burn your server and slow your app). It can be useful to store a venue's "Features" inside of this JSONB field since everything is super unstructured.
Lowering the number of attributes is better. You've got A LOT of them too which could slow down querying. I recommend you use Django-cachalot for caching since they support JSONField which can avoid your issue of having a LOT of attributes.
Other recommendations in general
Instead of using default='', just do blank=True, null=True since you're basically saying the user doesn't have to fill out the email field.
Kind of like how you would have a user profile instead of stuffing ALL of your attributes inside of the main User model, you want to partition your Venue data into different models.
The way I would've designed this:
Since you originally had these three venues, just make the "Categories" table into choices.
from django.contrib.gis.db import models # This also imports standard models
from django.contrib.postgres.fields import JSONField # Remember to turn on GeoDjango with PostgreSQL's PostGIS extension
from django.contrib.postgres.indexes import BrinIndex
class Venue(models.Model):
id = models.BigAutoField(primary_key=True)
title = models.CharField(max_length=255)
rating = models.DecimalField(default=10.0, max_digits=5, decimal_places=2)
created_date = models.DateTimeField(default=timezone.now)
approved_venue = models.BooleanField(default=False)
admin_seen = models.BooleanField(default=False)
VENUE_TYPES = [
(1, "restaurant"),
(2, "concert"),
(3, "art night")
]
category = SmallPositiveIntegerField(choices=VENUE_TYPES)
location = models.PointField(srid=4326) # mercator projection from GeoDjango. You don't have to use this; you can stick to your old city and address thing
class Meta:
indexes = (
BrinIndex(fields=['category']), # this is in case you have a LOT of categories later on.
)
class VenueProfile(models.Model):
venue = models.OneToOneField(Venue, on_delete=models.CASCADE, primary_key=True)
misc_features = JSONField() # This field is for stuff like your restaurant features OR your concert features. You can put whatever you want in there. Just make sure you have a list of features that people have when trying to access the JSON so you don't run into exceptions.
created_date = models.DateTimeField(auto_now_add=True)
facebook = models.CharField(max_length=100, blank=True, null=True)
instagram = models.CharField(max_length=100, blank=True, null=True)
city = models.ForeignKey(City, on_delete=models.SET_NULL, null=True) # SET_NULL in case you accidentally delete a city. You don't want to also delete the venue.
image = models.ImageField(upload_to='attachments',
verbose_name='Image')
# These attributes are universal for ANY venue so that's why they don't need to be in the JSONField
"""
For the rest of the features, I have no concern EXCEPT for city. Because you're using GeoDjango, you should also use MaxMind's free city database to determine location based on coordinates. That way, you've essentially scraped the need to store the user and such. You could probably save the address field since it could make things easier that a simple coordinate. It's really up to you. You could also use both!
"""
The attributes I've added to the Venue model are THE MOST important things in my opinion that a user would immediately want to know about.
The VenueFeature model is something that isn't updated that much. It's PRIME for using Django-cachalot to take over since it's not modified that often. (50 modifications per second makes invalidation of caches per modification a big hassle).
Comments model is fine.

How can i get in choices=max_guest by room_id when I create django.models?

I have model like this:
class KeyTransfer(Model):
key_out_data = DateTimeField(null=True, blank=True)
key_in_data = DateTimeField(null=True, blank=True)
room_id = ForeignKey(Room, blank=True, null=True, on_delete=CASCADE)
guests = IntegerField(choices=[(x, str(x)) for x in range(Room.objects.get(number=room_id).max_guests)], blank=True, null=True)
notes = CharField(max_length=256, blank=True)
person_id = ForeignKey(Person, null=True, blank=True, on_delete=SET_NULL)
and i understand that i can`t save some value in column "guests" with argument "choices=" as you can see above.
In a consequence I have gotten error:
...django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet. ...
and i see what this error means.
The question is: can I realise some similar condition for "choices" in "guests" do not using django.forms?
It seems that you want to set upper limit for guests field of KeyTransfer model based on its related room model's max_guests field. You can do that by overriding save method and check there if assigned guests is higher than max_guests. See below implementation of this approach:
from django.db import models
from django.core.exceptions import ValidationError
class Person(models.Model):
name = models.CharField(max_length=32)
def __str__(self):
return "<Person {}>".format(self.name)
class Room(models.Model):
max_guests = models.IntegerField()
class KeyTransfer(models.Model):
key_out_data = models.DateTimeField(null=True, blank=True)
key_in_data = models.DateTimeField(null=True, blank=True)
room_id = models.ForeignKey('Room', blank=True, null=True, on_delete=models.CASCADE)
guests = models.IntegerField(blank=True, null=True)
notes = models.CharField(max_length=256, blank=True)
person_id = models.ForeignKey('Person', null=True, blank=True, on_delete=models.SET_NULL)
def save(self, *args, **kwargs):
room = self.room_id
if self.guests > room.max_guests:
raise ValidationError("Assigned guests exceeding related room's maximum limit of {}"\
.format(room.max_guests))
super().save(*args, **kwargs)

How to write a self referencing Django Model?

I have a Django model "Inspection" which has:
InspectionID (PK)
PartID
SiteID
Date
Comment
Report
Signiture
I want to be able to have a one to many relationship between the inspection ID and date. So one ID can have inspections at many dates. How would I do this? I currently have the following:
class Inspection(models.Model):
InspectionID = models.IntegerField(primary_key=True, unique=True)
PartID = models.ForeignKey('Part', on_delete=models.CASCADE)
SiteID = models.ForeignKey('Site', on_delete=models.CASCADE)
Date = models.DateField(auto_now=False, auto_now_add=False)
Comment = models.CharField(max_length=255, blank=True)
Report = models.FileField(upload_to='docs', null=True, blank=True)
Signiture = models.CharField(max_length=255, blank=True)
I thought about using models.ForeignKey but I really don't know how to implement that properly in this situation.
I want to be able to have a one to many relationship between the inspection ID and date.
You create an extra model, like:
class InspectionDate(models.Model):
inspection = models.ForeignKey(
Inspection,
on_delete=models.CASCADE,
related_name='inspectiondates'
)
date = models.DateField()
You thus can create InspectionDates for a given Inspection.
Or if you want to add extra data, it might be better to define an InspectionGroup model:
class InspectionGroup(models.Model):
pass
class Inspection(models.Model):
id = models.AutoField(primary_key=True, unique=True, db_column='InspectionId')
inspectiongroup = models.ForeignKey(InspectionGroup, on_delete=models.CASCADE, db_column='InspectionGroupId')
part = models.ForeignKey('Part', on_delete=models.CASCADE, db_column='PartId')
site = models.ForeignKey('Site', on_delete=models.CASCADE, db_column='SiteId')
date = models.DateField(db_column='Date')
comment = models.CharField(max_length=255, blank=True, db_column='CommentId')
report = models.FileField(upload_to='docs', null=True, blank=True, db_column='ReportId')
signiture = models.CharField(max_length=255, blank=True, db_column='Signature')
Note: the name of attributes are normally written in snake_case [wiki], not in PerlCase or camelCase.
you may store 'self Foriegnkey' as
class Inspection(models.Model):
InspectionID = models.IntegerField(primary_key=True, unique=True)
PartID = models.ForeignKey('Part', on_delete=models.CASCADE)
SiteID = models.ForeignKey('Site', on_delete=models.CASCADE)
Date = models.DateField(auto_now=False, auto_now_add=False)
Comment = models.CharField(max_length=255, blank=True)
Report = models.FileField(upload_to='docs', null=True, blank=True)
Signiture = models.CharField(max_length=255, blank=True)
inspection_id = models.ForeignKey('self', null=True, blank=True)

Is a self-join on a model in a django app an acceptable pattern?

Apologies if this question is too subjective.
If you are planning to close this question: please comment with a suggestion for a more appropriate place to post.
I'm super new to django and python, and I'm building a test app that keeps track of employees and who their managers are.
I would like to set up the domain model so that there there is only one list of employees, any of which can be managers, and all of which can be managed by any other employee who is designated a manager.
To achieve this, I did a self-join on the Employee model and have an "is_manager" flag to keep track of who is a manager and who isn't (see model below).
Is an acceptable pattern?
I'm worried it violates a design principle I'm not considering and there's some hairy trap that I'm walking into as a noob.
Thank you very much for your time.
models.py for the app:
class OrganizationTitle(models.Model):
def __str__(self):
return "{}".format(self.organization_title_name)
organization_title_name = models.CharField(max_length=150, unique=True)
class ClassificationTitle(models.Model):
def __str__(self):
return "{}".format(self.classification_title_name)
classification_title_name = models.CharField(max_length=150, unique=True)
class WorkingTitle(models.Model):
def __str__(self):
return "{}".format(self.working_title_name)
working_title_name = models.CharField(max_length=150, unique=True)
class Category(models.Model):
def __str__(self):
return "{}".format(self.category_name)
category_name = models.CharField(max_length=150, unique=True)
class Department(models.Model):
def __str__(self):
return "{}".format(self.department_name)
department_name = models.CharField(max_length=150, unique=True)
class Employee(models.Model):
first_name = models.CharField(max_length=150)
last_name = models.CharField(max_length=150)
org_title = models.ForeignKey(OrganizationTitle, blank=True, null=True, on_delete=models.SET_NULL)
manager = models.ForeignKey('self', null=True, blank=True, on_delete=models.SET_NULL)
manager_email = models.EmailField(max_length=50, blank=True, null=True)
hire_date = models.DateField(blank=True, null=True)
classification_title = models.ForeignKey(ClassificationTitle, blank=True, null=True, on_delete=models.SET_NULL)
working_title = models.ForeignKey(WorkingTitle, blank=True, null=True, on_delete=models.SET_NULL)
email_address = models.EmailField(max_length=250, blank=False, unique=True,
error_messages={'unique': 'An account with this email exist.',
'required': 'Please provide an email address.'})
category = models.ForeignKey(Category, blank=True, null=True, on_delete=models.SET_NULL)
is_substitute = models.BooleanField(default=False)
department = models.ForeignKey(Department, blank=True, null=True, on_delete=models.SET_NULL)
is_active = models.BooleanField(default=True)
is_manager = models.BooleanField(default=False)
class Meta:
ordering = ('is_active', 'last_name',)
def __str__(self):
return "{}".format(self.first_name + ' ' + self.last_name)
That's perfectly fine.
I would recommend you to specify the related_name to keep your code more explicit:
manager = models.ForeignKey(..., related_name="managed_employees")
so then you can do something like:
bob.managed_employees.all()
Also, there are 2 things I would change (not your question but still regarding the models):
1.The manager_email field is redundant. I would remove it. You already have that information at tom.manager.email_address for example.
2.There are many fields that I would simply rename to name. For example:
class OrganizationTitle(models.Model):
def __str__(self):
return u"{}".format(self.name)
name = models.CharField(max_length=150, unique=True)
No need to call it organization_title_name. That's consistent with the first_name field (not employee_first_name).
Yes, this is an acceptable pattern. This is called a "recursive relationship", or "self referential foreign keys" and is a very common usecase in realworld applications.
Here is django's example supporting this usecase

Categories