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.
Related
In my models, I added the class Source:
class Source(models.Model):
profile = models.ForeignKey('Profile', on_delete=models.CASCADE, null=True, default=False, blank=True, related_name='+')
project= models.ForeignKey('Project', on_delete=models.CASCADE, null=True, default=False, blank=True, related_name='+')
team = models.ForeignKey('Team', on_delete=models.CASCADE, null=True, default=False, blank=True, related_name='+')
department = models.ForeignKey('Department', on_delete=models.CASCADE, default=False, blank=True, null=True, related_name='+')
def __str__(self):
return str(self.id)
For testing purposes, I added a few entries for posts in the Django admin page,
and then went back into my classes: Post, Profile, Department, Team, and Project, and added:
sourceID = models.ForeignKey('Source', default='1', on_delete=models.CASCADE, related_name='+', null=False)
for each class.
My problem is that when I go in the Django admin page to alter the database (i.e. create a new post) I get the following error:
Also, when I go to migrate my changes, I keep getting this warning:
HINT: Configure the DEFAULT_AUTO_FIELD setting or the LeadsConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
I would like to be able to create new entries in my database without the operational error. I've tried changing the null value = True, but this is counterintuitive and doesn't yield any change, surprisingly. I think the best way would be to delete the whole database, which I've tried to research and only found MYSql code solutions. What is the best course of action?
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
Migrating my Access app to Python/Django. Existing database, so used Inspectdb to create models, which I adjusted as needed until they matched actual db. All models marked Manage = False in Meta.
Now I want to add a many-to-many field in a parent table, and having troubles. Snippets of models:
class Federalprograms(models.Model):
fedpgmid = models.IntegerField(db_column='FedPgmID', primary_key=True) # Field name made lowercase.
fedpgmname = models.CharField(db_column='FedPgmName', max_length=50) # Field name made lowercase.
active = models.IntegerField(db_column='Active', blank=True, null=True, default=0) # Field name made lowercase.
seq = models.IntegerField(db_column='Seq', blank=True, null=True, default=0) # Field name made lowercase.
loanfee = models.DecimalField(db_column='LoanFee', max_digits=10, decimal_places=5, blank=True, null=True, default=0.00000) # Field name made lowercase.
class Reports(models.Model):
reportname = models.CharField(db_column='ReportName', primary_key=True, max_length=50) # Field name made lowercase.
reporttitle = models.CharField(db_column='ReportTitle', max_length=50, blank=True, null=True) # Field name made lowercase.
datefielddefault = models.IntegerField(db_column='DateFieldDefault', blank=True, null=True) # Field name made lowercase.
startdatedefault = models.CharField(db_column='StartDateDefault', max_length=15, blank=True, null=True) # Field name made lowercase.
enddatedefault = models.CharField(db_column='EndDateDefault', max_length=15, blank=True, null=True) # Field name made lowercase.
detailsdefault = models.BooleanField(db_column='DetailsDefault', max_length=1, blank=True, null=False) # Field name made lowercase.
totalsdefault = models.BooleanField(db_column='TotalsDefault', max_length=1, blank=True, null=False) # Field name made lowercase.
askfedpgm = models.BooleanField(db_column='AskFedPgm', blank=True, null=False, default=0) # Field name made lowercase.
fedpgms = models.ManyToManyField('Federalprograms')
So I added the "fedpgms" field, then did makemigrations which seemed to work, and then did migrate, which also seemed to work. But, the join table, reports_fedpgms was not created, and when I run the application I get the following error:
ProgrammingError at /admin/fc6/reports/StatusReport/change/
(1146, "Table 'fc5.reports_fedpgms' doesn't exist")
I tried making the Reports table manage=True, thinking this might trigger creation of the join table, but that only caused an error during migrating, saying "Reports table already exists".
I'm kind of new to this Django thing, what am I doing wrong?
Thanks...
Change it to
fedpgms = models.ManyToManyField(Federalprograms)
I am new to programming Django, so I'm not sure if this is possible.
I have created a new CustomUser class:
class CustomUser(AbstractBaseUser):
email = models.EmailField(verbose_name='email address', max_length=255, unique=True)
mobile = models.CharField(max_length=30, null=True)
first_name = models.CharField(max_length=50, null=True)
middle_name = models.CharField(max_length=50, null=True)
last_name = models.CharField(max_length=50, null=True)
date_of_birth = models.DateField(null=True)
Primary_address = models.CharField(max_length=50, null=True)
Primary_address_zipcode = models.CharField(max_length=5, null=True)
is_active = models.BooleanField(default=False)
is_admin = models.BooleanField(default=False)
date_joined = models.DateTimeField(default=timezone.now)
A few questions:
Question 1: I have redefine some of the fields that exists in the default User class (e.g. First Name, Last name, Date Joined). However, I didn't define Last_login. But last_login still shows up as a column in the admin page. But if I don't define First Name, Last Name and Date Joined in my new CustomUser, I get an error and doesn't show up in the admin page. Why is last login special?
Question 2: The default admin page has great UI for group and permission control. Is it possible to define my CustomerUser and still use/enable the default admin page?
Thanks.
you dont need to define all these fields that are already there in django. dont reinvent the wheel. what error are you getting? traceback?
using your customuser model has nothing to do with using default admin page. you can always use django admin page no matter what models you have. Or i dont understand what you really want to achieve.