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)
Related
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'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 the following code in models.py, note the function at the bottom that seeks to change the teacher_object in the admin panel to the username of the teacher instead.
class Teachers(models.Model):
email = models.CharField(max_length=50)
school_pin = models.CharField(max_length=11)
username = models.CharField(max_length=50)
pass_field = models.CharField(db_column='pass', max_length=100) # Field renamed because it was a Python reserved word.
fname = models.CharField(max_length=50, blank=True, null=True)
lname = models.CharField(max_length=50, blank=True, null=True)
oauth_provider = models.CharField(max_length=50, blank=True, null=True)
oauth_uid = models.CharField(max_length=100, blank=True, null=True)
verified = models.IntegerField(blank=True, null=True)
verificationcode = models.CharField(db_column='verificationCode', max_length=100, blank=True, null=True) # Field name made lowercase.
school_name = models.CharField(max_length=255, blank=True, null=True)
designation = models.CharField(max_length=255, blank=True, null=True)
date = models.DateField(blank=True, null=True)
membershipid = models.IntegerField(blank=True, null=True)
def __str__(self):
return self.username
class Meta:
managed = False
db_table = 'teachers'
The admin panel still shows the teachers listed as Teachers object (1275) (and so on).
I have tried the following:
1. Running the makemigrations and migrate commands
2. Logging in and out of the admin panel
3. Refreshing and closing down CMD - restarting
None of the above has worked. There are no errors on running the server.
Python version: 3.7 Latest Django installation
Considerations:
1. I was using a legacy mysql database so reverse created the models. Could this be affecting anything?
2 I wonder if the class Meta (at the bottom) has any bearing on the error.
I also tried the following, but still no result:
def __str__(self):
return '%s %s' %(self.username,self.email)
For reference, and in case this is relevant here is my admin.py
from django.contrib import admin
# Register your models here.
from django.contrib import admin
from .models import Teachers
from .models import TeacherPins
admin.site.register(Teachers)
admin.site.register(TeacherPins)
Finally, as this is a reverse-generated model (from a legacy mysql) Django provided this advice at the start of the models.py file. Again, could this (not being done, e.g not having a primary key etc) have a bearing on the str method not working?
# This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
# * Rearrange models' order
# * Make sure each model has one field with primary_key=True
# * Make sure each ForeignKey has `on_delete` set to the desired behavior.
# * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table
# Feel free to rename the models, but don't rename db_table values or field names.
Update:
I did try rearranging the order (of the classes) - would this make a difference? But still the object remained an object
I tried adding a primary key to both classes - this didn't work either
student_id = models.IntegerField(blank=True, null=True, primary_key=True)
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 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.