I am developing Django application, but still have confusion over apps design pattern, let say my application has models like follow.
class Department(models.Model):
name = models.CharField(max_length=255)
class Student(models.Model):
name = models.CharField(max_length=255)
department = models.ForeignKey(Department)
As u see student model has relation of department = models.ForeignKey(Department)
In This case should i need to create separate apps for department and student or is it good enough to create custom_app with both department and student models ?
You don't have to create app for each model. App is more high level thing. You can logically think of app name which contains both models: for example, 'university' or 'practice' or even 'students' that will contain all the business logic of interaction with this models. Below you can create another app that could have any other models. Just try to link each model to only one app if you can
Related
I have a normal Django model. I want to give a certain user the ability to add custom fields. I want the model to update during runtime and the database migrations to apply and then the user can do regular CRUD operations.
Example of a simple Model is
class location(models.Model):
AuotLocationID = models.CharField(max_length=200)
LocationID = models.CharField(max_length=200)
Name = models.CharField(max_length=200)
The user should be able to add other model fields such as Postal Code, Population, etc. Also each instance of the model should be linked to each user. For example Bob may want to track certain fields for his location and John may want some other fields.
Thank you for your help
I'm am building a django app which takes user Interests as inputs.
Now I have 2 Questions -
First is that, what model should I use, should I just add a field to user model or a separate Interest Model and link via Foreign Key?
I know the former design is bad, and so I.m trying latter one, I'm having a hard time in Django to create Interest Model and its view to save the user interests.
Any help is appreciated.
I am trying to accomplish the same thing.
Here is how I have solved it:
I have not tried it out yet, but this should work as a solution.
from django.contrib.auth.models import User
class Nation(models.Model):
name=models.CharField(max_length=64)
class Subject(models.Model):
name=models.CharField(max_length=64)
class Interests(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
nationals = models.ManyToManyField(Nation)
subjects = models.ManyToManyField(Subject)
I have 3 apps products, sales, purchases. each app has a correspondingly named Model class, Product, Sale, and Purchase.
products/models.py
class Product(models.Model):
Name = models.CharField(max_length=32)
sales/models.py
class Sale(models.Model):
Product = models.ForeignKey('products.Product', on_delete=models.CASCADE)
purchases/models.py
class Purchase(models.Model):
Product = models.ForeignKey('products.Product', on_delete=models.CASCADE)
And I decided to make custom managers for the Model classes so that I can keep all the logic in the model files (by overriding objects attr for each class) when I'm writing the methods in the custom manager I imported Sale model In products.models and Product model in sales.models which creates a circular reference, I was able to get away with it by performing the imports in the methods themselves but I remember reading online that circular imports are sign of bad code writing.
So my question is how can I avoid circular imports in this case and have access to the Product Model in sales.models and Sale in products.models.
You can import a model by name to avoid circular imports. When you need to use the model, import it like this:
from django.apps import apps
ModelName = apps.get_model(app_label='app_name', model_name='ModelName')
Running into circular importing means you are fighting against the separation you've created, or you're not writing appropriately decoupled code. In your case, I think you may be trying too hard to separate these models into separate apps. Surely sales and purchases are part of the same set of functionality and share lots of business logic. Even products could live in the same app, although you might have more of an argument there.
If you're coming from another language where it's customary not to have multiple classes in a single file, you should know that is not a best practice in python. Note in the official Django tutorial, the polls app has both Question and Answer models in the same file.
I think a single app named commerce with all three models in the same models.py file would make sense.
# commerce/models.py
class Product(models.Model):
name = models.CharField(max_length=32)
class Sale(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
class Purchase(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
I want to add auto-service, it has list of brands of cars and services in M2M field.
For each brand of car I should link several services.
Please help me to write code I'm a beginner trying to make website.
here is url to picture, take a look
https://ibb.co/31DZZJ1
Well,I don't want to write everything. But Basically, you will have two models.
class CarBrand(models.Model):
name = models.CharField(max_length=200)
services = models.ManyToManyField('Services', blank=True)
class Services(models.Model):
service_name = models.CharField(max_length=200)
Then you can add these models to your admin and once you fill the services model admin will show you a list of services and you can select the ones you want for each car brand in the car brand model..
Sorry I am not good at explaining. but ask me if you don't understand
I have a Django project with two apps that are almost identical but for a number of reasons, I'd like to keep them separate. To keep things DRY, I created a base_model file.
# base_model.py
from django.db import models
class ModelOne(models.Model):
name = models.CharField()
class ModelTwo(models.Model):
name = models.CharField()
friend = models.ForeignKey(ModelOne)
My two apps are called app1 & app2.
How do I import these models so that essentially I'll have 4 tables for these two models?
Thanks.
UPDATE 1
The reason why I need two versions of these 2 models is that I want to keep them in a separate database as they have separate uses.
For example, let's say I own a home with furniture and as well as a furniture store. I want to keep a separate list for both sets of furniture. The furniture in my home will be kept on a home server, while my store furniture will be kept on a work server. In addition, I'll also like to keep track of the color of the furniture, so that will be a separate table.
# base_model.py
from django.db import models
class Color(models.Model):
name = models.CharField()
class FurnitureBase(models.Model):
name = models.CharField()
color = models.ForeignKey(Color)
For the store app, I need to keep track of pricing, manufacturer, etc. For the home app, I'll need to keep track of usage, age, etc.
These two models should be defined as abstract = True under Meta. Then in each of your two apps, sub-class to create concrete versions in each app. Something like:
app1/models.py:
class App1ModelOne(base_models.ModelOne):
pass
class App1ModelTwo(base_models.ModelTwo):
pass
Read abstract base classes and model inheritance in django's docs for more information: https://docs.djangoproject.com/en/1.7/topics/db/models/#abstract-base-classes