I have a project that has 2 different apps. One handles a site's performance metrics and the other estimates site's workload. The workload app has some dependencies on the performance app. However, I am trying to avoid creating a dependency from the metrics app to the workload app in order to better encapsulate the metrics app as stand-alone. For example, metrics has this model:
# models.py in metrics app:
class Site(models.Model):
site_name = models.CharField(max_length=255)
site_geography = models.CharField(max_length=255)
slug = AutoSlugField(populate_from='site_name', unique=True, null=True)
Workload app has objects such as (some omitted for brevity):
# models.py in workload app:
class Resource (models.Model):
name_first = models.CharField(max_length=60)
name_last = models.CharField(max_length=60)
address_street = models.CharField(max_length=80, blank=True)
address_city = models.CharField(max_length=40, blank=True)
address_state = models.CharField(max_length=2, blank=True)
sites = models.ManyToManyField(metrics_models.Site, through='ResourceSiteRelationship', blank=True, related_name='resources')
class Activity (models.Model):
activity_name = models.CharField(max_length=255)
hours = models.DecimalField(max_digits=5, decimal_places=2)
travel = models.BooleanField()
description = models.TextField(blank=True)
class ResourceSiteRelationship (models.Model):
roles = models.ManyToManyField(Role, blank=True, related_name='resource_site_relationships')
resource = models.ForeignKey(Resource, on_delete=models.CASCADE, blank=True, null=True, related_name='resource_site_relationships')
site = models.ForeignKey(metrics_models.Site, on_delete=models.CASCADE, blank=True, null=True, related_name='resource_site_relationships')
Site should also have the following field relationship:
activities = models.ManyToManyField(Activity, related_name='sites', blank=True)
Is it best to add that field to the metric's app's instance of Site? Is it better to create a one-to-one class for Site in the workload app and extend like so:
class Site(models.Model):
site = models.OneToOneField(metrics_models.Site, on_delete=models.CASCADE)
activities = models.ManyToManyField(Activity, related_name='sites', blank=True)
Or is there another way to do this?
Related
I'm working on a big project for which I need to breakdown my application into different maintainable modules and I keep getting this error whenever I'm doing imports across different modules of the application.
django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: admin
for now I have a module named asset_herarchi and another module name db_configurations
In asset_herarchi I've the following two models:
from db_configurations.models import TableDataReferenceConfiguration
class Attribute(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=100, blank=False, null=False)
description = models.CharField(max_length=500, blank=True, null=True)
table_datareference_config = models.ForeignKey(TableDataReferenceConfiguration, related_name="data_reference_where_conditions", on_delete=models.CASCADE)
class Unit(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=150, blank=False, null=False)
description = models.CharField(max_length=500, blank=True, null=True)
formula = models.CharField(max_length=50, blank=True, null=True)
In db_configurations I've got the following model.
from asset_herarchi.models import Unit
class TableDataReferenceConfiguration(models.Model):
id = models.UUIDField(default=uuid.uuid4, primary_key=True, editable=False)
table_name = models.CharField(max_length=100, null=False, blank=False, unique=False)
result_column_name = models.CharField(max_length=100, null=False, blank=False, unique=False)
unit_of_measure = models.ForeignKey(Unit, related_name="UOM_Table_Data_Reference_Configuration", on_delete=models.SET(None) )
behavior_rule = models.CharField(choices=Rule, null=False, blank=False, max_length=20)
behavior_order_by = models.CharField(max_length=100, null=True, blank=True, unique=False)
behavior_order_sorting = models.CharField(choices=Sorting, null=True, blank=True, max_length=20)
As I've mentioned earlier I'm splitting the application into different modules for easy maintenance
For some business logic requirements I need to import the Unit model into the db_configurations.model file to create a relationship with TableDataReferenceConfiguration and I need to import the TableDataReferenceConfiguration model back into the asset_herarchi.models to create a relationship with Attribute model and at the same time I wish the Model TableDataReferenceConfiguration to be in a separate module for some business requirements.
Due to these imports between multiple modules I'm having the error: django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: admin
when I comment the import from anyone of the .models file the core proceeds. can someone help me with this error?
I have a class in models.py :
class Customer(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, null=True)
name = models.CharField(max_length=200, null=True, blank=True)
email = models.CharField(max_length=200, null=True, blank=True)
device = models.CharField(max_length=200, null=True, blank=True)
Is there a way to access this model in navbar without creating entry in "views.py"? I would like to access similarly to {{ request.user.id }}.
Customer is related to User using One-to-one relationship, you can get it through User object
request.user.customer
EDIT (after determining Customer and User are not really related):
You can write your own context processor which will return Customer object
I am currently working on django 2.0.2 admin page. I have three tables, which are 'metabolites', 'gene' and 'reactions.' The structure of each class is defined as below:
class Genes(models.Model):
id = models.CharField(primary_key=True, max_length=255)
name = models.CharField(max_length=255, blank=True, null=True)
notes = models.CharField(max_length=255, blank=True, null=True)
class Meta:
managed = False
db_table = 'Genes'
class Metabolites(models.Model):
id = models.CharField(primary_key=True, max_length=255)
name = models.CharField(max_length=255, blank=True, null=True)
compartment = models.CharField(max_length=255, blank=True, null=True)
charge = models.CharField(max_length=255, blank=True, null=True)
formula = models.CharField(max_length=255, blank=True, null=True)
notes = models.CharField(max_length=255, blank=True, null=True)
class Meta:
managed = False
db_table = 'Metabolites'
class Reactions(models.Model):
id = models.CharField(max_length=255, primary_key=True)
name = models.CharField(max_length=255, blank=True, null=True)
metabolites = models.TextField(blank=True, null=True)
lower_bound = models.CharField(max_length=255, blank=True, null=True)
upper_bound = models.CharField(max_length=255, blank=True, null=True)
gene_reaction_rule = models.TextField(blank=True, null=True)
subsystem = models.CharField(max_length=255, blank=True, null=True)
notes = models.CharField(max_length=255, blank=True, null=True)
class Meta:
managed = False
db_table = 'Reactions'
As you can see, the 'reaction' class also included 'metabolites' component. A typically reaction actually involved more than two metabolites. What I want to do is, create a search field on the admin page(not the page of each class), and when I type in the reaction id, the searching result can display the reaction and all the involved metabolites, and when I type in a metabolites, the searching result can display this metabolite's information and all reactions this metabolites involved.
Is that possible? Can somebody tell me how to do this?
Thank you for helping me!
EDIT:
This describes the "old school" way of accomplishing this. This appears to be a use case for django's many to many fields. I have not run into this need in my project; so, I have not, yet, studied up the many to many capabilities in django. I recommend reading the django docs for how to use many to many fields.
The way described here will accomplish the desired connections in the data. However, I suspect that the django admin will be easier and more straightforward to set up using a many to many field.
end edit
You want to make another model for metabolites_in_reaction that only contains its own primary key, a foreign key to the reaction and foreign key to metabolites.
class ReactionMetabolites(models.Model):
reaction = models.ForeignKey(Reactions, on_delete=models.CASCADE)
metabolite = models.ForeignKey(Metabolites, on_delete=models.CASCADE)
A many to many field may also be appropriate here; I have not really figured out the many to many fields yet.
Edit 2:
After making these changes in your models, you will need to make and apply migrations to apply the changes to your database.
python manage.py makemigrations
python manage.py migrate
models.py as below,
from django.db import models
from django.contrib.auth.models import User
class members(models.Model):
auto_id = models.AutoField(primary_key=True)
member_name = models.OneToOneField(User)
owner = models.ForeignKey('auth.User', related_name='webapi', on_delete=models.CASCADE)
father_name = models.CharField(max_length=25, blank=False, default='')
wife_name = models.CharField(max_length=25, blank=False, default='')
number_of_child = models.IntegerField(blank=True)
address_line_1 = models.CharField(max_length=40, blank=False, default='')
address_line_2 = models.CharField(max_length=40, blank=True, default='')
city = models.CharField(max_length=25, blank=False, default='')
class Meta:
ordering = ('member_name',)
Now The above has been linked with django auth Users table and in steriliser it shows the existing Django users and I have to choose one during the submit.
But my requirement is as a admin user I login and then provide the member_name manually which should automatically create a django user also
django-annoying has a feature for this, specifically AutoOneToOneField.
Sample from their github:
from annoying.fields import AutoOneToOneField
class MyProfile(models.Model):
user = AutoOneToOneField(User, primary_key=True)
This should automatically create a User.
I have this two models:
class Folder(MPTTModel):
name = models.CharField(max_length=20)
description = models.CharField(max_length=200, null=True, blank=True)
parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
position = models.IntegerField(default=0)
class MPTTMeta:
order_insertion_by = ['position']
class Page(MPTTModel):
title = models.CharField(max_length=50)
file_content = models.TextField()
parent = TreeForeignKey(Folder, null=False, blank=False, related_name='page', on_delete=models.CASCADE)
I have been trying and trying, but I can't find any solution.
Is there anyway to merge those two models in just one tree diagram?
The idea is that a Folder can contain a Folder or a Page
I'm using django 1.6.5 and python3
Maybe you should create an abstract superclass. Then you can have this class from which both (folder and Page) inherit and you can define the parent relation already in there.