I have two tables that have already been populated(~1700 entries each):
class Drug(models.Model):
drug_id = models.AutoField(primary_key=True)
name_generic = models.TextField(blank=True, null=True)
indication = models.TextField(blank=True, null=True)
approval_company = models.TextField(blank=True, null=True)
class Company(models.Model):
name = models.TextField(primary_key=True)
research = NullBooleanField()
successor = models.TextField(blank=True, null=True)
founded = models.DateField(blank=True, null=True)
I have it set up so that approval_company in Drug is a match to name in Company. What I would like to do is to make the approval_company field on Drug that has a Foreign Key relationship to Company.
After setting
approval_company = models.ForeignKey(Company), and then adding the constraint in postgres, I get a ProgrammingError whenever I try to visit the django admin page for Drug.
Django tries to look for an approval_company_id field that does not exist. How would I go about adding this Foreign Key relationship on a table that already has many values?
Related
I have a condition that has one to many relationship scenarios because I will have multiple projects inside one account.
models.py
class Account(models.Model):
name = models.CharField(max_length=255, default='')
class Project(models.Model):
account = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
how can I manage this scenario, currently I'm getting the following error:
django.db.utils.ProgrammingError: relation "project_account_id_7d9b231b" already exists
account = models.ForeignKey(Account, on_delete=models.CASCADE, null=True)
I am trying to map multiple field to same field in vendor and menu class. If I map using foreign key like below it works. But this is not what I want.
class OrderItem_Mon(models.Model):
vendor_name = models.ForeignKey(Vendor, on_delete=models.SET_NULL, null=True)
menu_name = models.ForeignKey(Menu, on_delete=models.SET_NULL, null=True)
date_created = models.DateTimeField('date created', auto_now_add=True)
note = models.CharField('note', max_length=255, null=True, blank=True)
I need to map multiple field to same field of specific table like this.
class OrderItem_Mon(models.Model):
vendor_name_1 = models.ForeignKey(Vendor, db_column='vendor_name', on_delete=models.SET_NULL, null=True)
menu_name_1 = models.ForeignKey(Menu, db_column='menu_name',on_delete=models.SET_NULL, null=True)
vendor_name_2 = models.ForeignKey(Vendor, db_column='vendor_name',on_delete=models.SET_NULL, null=True)
menu_name_2 = models.ForeignKey(Menu, db_column='menu_name', on_delete=models.SET_NULL, null=True)
date_created = models.DateTimeField('date created', auto_now_add=True)
note = models.CharField('note', max_length=255, null=True, blank=True)
However, it does not work. How do I make this work? Basically, I need to make new form that have dropbox that gets value from vendor and menu model to each field. Help
You need to add related_name attribute for foreign key model fields and give different names.
The error is due to the same model used as a Foreign key for multiple fields. So.it makes an issue during migration. Just setting the different .related_name wont make any issue.
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
I have two models as shown below. ArticlePost is related to Article by a Foreign Key. I'm wanting to remove name as a primary key on Article and instead have the Django default id field. What is the best steps to do this so that all related ArticlePosts will maintain the correct Foreign Key to Article?
class Article(models.Model):
name = models.CharField(max_length=200, primary_key=True)
class ArticlePost(models.Model):
article = models.ForeignKey(Article, null=False, db_index=True)
created_at = models.DateTimeField(auto_now_add=True)
comment = models.TextField(blank=True)
In need of some dire help with setting up relationships in my django model and views.
Just wanted to say thank you! before anyone takes a deep dive below!
Working on an application were I have a One to Many relationship where I have many Products and a few particular products will be related to only one Website.
One of the biggest problems am experiencing is when I try to add a foreign key to my Website Model I get this error:
? The field 'Product.website' does not have a default specified, yet is NOT NULL.
? Since you are adding this field, you MUST specify a default
? value to use for existing rows. Would you like to:
? 1. Quit now, and add a default to the field in models.py
? 2. Specify a one-off value to use for existing columns now
I tried to use this solution here:
Django South - Create Not Null ForeignKey
But to no avail, didnt know what to do after Step #4 and I just got lost regardless.
Models.py in my product_extend app
Product Model:
class Product(models.Model):
"""
The product structure for the application, the products we scrap from sites will model this and save directly into the tables.
"""
product_name = models.CharField(max_length=254, verbose_name=_('Name'), null=True, blank=True)
product_price = CurrencyField( verbose_name=_('Unit price') )
product_slug_url = models.URLField(max_length=200, null=True, blank=True)
product_category = models.CharField(max_length=254, blank=True, null=True)
product_img = models.ImageField('Product Image', upload_to='product_images', null=True, blank=True)
product_website_url = models.URLField(max_length=200, null=True, blank=True)
product_website_name = models.CharField(max_length=254, blank=True, null=True)
#For Admin Purposes, to keep track of new and old items in the database by administrative users
date_added = models.DateTimeField(auto_now_add=True, null=True, blank=True, verbose_name=_('Date added'))
last_modified = models.DateTimeField(auto_now=True, null=True, blank=True, verbose_name=_('Last modified') )
#For Admin Purposes, to make sure an item is active by administrative users
active = models.BooleanField(default=True, verbose_name=_('Active') )
# Foreign Key
website = models.ForeignKey(Website, null=True, related_name='website_to_product')
Website Model
class Website(models.Model):
name = models.CharField(max_length=254, blank=True, null=True, unique=True)
description = models.TextField(null=True, blank=True)
website_slug = models.SlugField(verbose_name=_('Website Slug'), unique=True)
site_logo = models.ImageField('Websites Logo', upload_to='website_logo_images', null=True, blank=True)
menswear = models.BooleanField(default=False, verbose_name=_('Menswear'))
womenswear = models.BooleanField(default=False, verbose_name=_('Womenswear'))
active = models.BooleanField(default=True, verbose_name=_('Active'))
Edit
I shortened this question in an effort to make it more comprehensible and split the second part as another question:
Django 1.6: Displaying a particular models Objects in another template
Is it right to say that when you do the migration, both Product and Websites already exist in models.py? If that is the case, a workaround would be to enter a random value for this
? 2. Specify a one-off value to use for existing columns now
Or you may for the time being set the null to be true. Then after you created the foreign key and added the key values for the existing rows. You can re-set it to be false. Then run the migration process as in https://stackoverflow.com/a/22617012/2774853:
Step 6. Run ./manage.py schemamigration <app> --auto
Step 7. Run ./manage.py migrate <app>
Hope it helps.