How to add a column in table from frontend in django - mysql

I want to add columns to my Django app from frontend. For this, I have created a separate table for table fields but I don't know how can I add a field so that it automatically becomes a field in Contact table and how can i make a dynamic form to ask to fill such inputs.
Thanks in advance.
class Contact(models.Model):
fname = models.CharField(max_length=200)
lname = models.CharField(max_length=200)
email = models.CharField(max_length=100)
mobile = models.CharField(max_length=12)
address = models.CharField(max_length=200,default=None)
pincode = models.CharField(max_length=10)
created_date = models.DateField(auto_now=True)
class Field(models.Model):
field_name = models.CharField(max_length=100)
field_type = models.CharField(max_length=50)
is_required = models.BooleanField()

Related

is there a way to change the date format in django/html?

i am still new to django and i am following a tutorial but when the guy i had the error the method he used didnt work for me - i am get this error althogh the default format for date is yyyy- mm-dd - any help will be appreciated
['“” value has an invalid date format. It must be in YYYY-MM-DD
format.']
this is my save function that inputs the data into the database
def add_seniorEmployee_save(request):
if request.method!="POST":
return HttpResponse("Method Not Allowed")
else:
first_name=request.POST.get("first_name")
last_name=request.POST.get("last_name")
username=request.POST.get("username")
email=request.POST.get("email")
password=request.POST.get("password")
Dob=request.POST.get("Dob")
Nationality=request.POST.get("Nationality")
Address=request.POST.get("Address")
Postcode=request.POST.get("Postcode")
Telephone=request.POST.get("Telephone")
Wage=request.POST.get("Wage")
Passportnumber=request.POST.get("Passportnumber")
passportexpirydate=request.POST.get("passportexpirydate")
gender=request.POST.get("gender")
kinname=request.POST.get("kinname")
kinrelation=request.POST.get("kinrelation")
kinaddress=request.POST.get("kinaddress")
kinphonenumber=request.POST.get("kinphonenumber")
kinemail=request.POST.get("kinemail")
#try:
user = CustomUser.objects.create_user(username=username,password=password,email=email,first_name=first_name,last_name=last_name,user_type=2)
user.senioremployee.Dob = Dob
user.senioremployee.Nationality = Nationality
user.senioremployee.Address = Address
user.senioremployee.Postcode = Postcode
user.senioremployee.Telephone = Telephone
user.senioremployee.Wage = Wage
user.senioremployee.Passportnumber = Passportnumber
user.senioremployee.passportexpirydate = passportexpirydate
user.senioremployee.gender = gender
user.senioremployee.profile_pic=""
user.senioremployee.kinname = kinname
user.senioremployee.kinrelation = kinrelation
user.senioremployee.kinaddress = kinaddress
user.senioremployee.kinphonenumber = kinphonenumber
user.senioremployee.kinemail = kinemail
user.save()
this is the model for my senior employee who i am adding to the database
this is the model to create the customer user
class CustomUser(AbstractUser):
user_type_data=((1,"Admin"),(2,"senioremployee"),(3,"employee"))
user_type=models.CharField(default=1,choices=user_type_data,max_length=20)
class senioremployee(models.Model):
id = models.AutoField(primary_key=True)
admin=models.OneToOneField(CustomUser, on_delete=models.CASCADE)
Dob = models.DateField()
Nationality = models.TextField()
Address = models.TextField()
Postcode = models.TextField()
Telephone = models.TextField()
Wage = models.TextField()
Passportnumber = models.TextField()
passportexpirydate = models.DateField()
gender = models.CharField(max_length=255)
profile_pic = models.FileField()
kinname = models.TextField()
kinrelation = models.TextField()
kinaddress = models.TextField()
kinphonenumber = models.TextField()
kinemail = models.TextField()
created_at=models.DateTimeField(auto_now_add=True)
updated_at=models.DateTimeField(auto_now_add=True)
objects = models.Manager()
problem is with this line Dob=request.POST.get("Dob") you are saving a string in Dob , you need to convert it to date format. use strptime() from datetime module for conversion to date object and then save to the database.

inputs are not being stored in Mysql database

i am currently having an issue with my program my some of my input are not being saved in the MySQL database - any help will be appreciated
this creates a custom user which are the employee, admin and senior employee - this is a different model so another table
class CustomUser(AbstractUser):
user_type_data=((1,"Admin"),(2,"senioremployee"),(3,"employee"))
user_type=models.CharField(default=1,choices=user_type_data,max_length=20)
this the model for one of my usertypes
class senioremployee(models.Model):
id = models.AutoField(primary_key=True)
admin=models.OneToOneField(CustomUser, on_delete=models.CASCADE)
Dob = models.DateField()
Nationality = models.TextField()
Address = models.TextField()
Postcode = models.TextField()
Telephone = models.TextField()
Wage = models.TextField()
Passportnumber = models.TextField()
passportexpirydate = models.DateField()
gender = models.CharField(max_length=255)
profile_pic = models.FileField()
kinname = models.TextField()
kinrelation = models.TextField()
kinaddress = models.TextField()
kinphonenumber = models.TextField()
kinemail = models.TextField()
created_at=models.DateTimeField(auto_now_add=True)
updated_at=models.DateTimeField(auto_now_add=True)
objects = models.Manager()
this function add user's data to the table
#receiver(post_save,sender = CustomUser)
def create_user_profile(sender,instance,created,**kwargs):
if created:
if instance.user_type==1:
admin.objects.create(admin = instance,Dob="",Nationality="",Address="",Postcode="",Telephone="",Wage="",Passportnumber="",passportexpirydate="",gender="",profile_pic="",kinname="",kinrelation="",kinaddress="",kinphonenumber="",kinemail = "")
if instance.user_type==2:
senioremployee.objects.create(admin = instance,Dob="",Nationality="",Address="",Postcode="",Telephone="",Wage="",Passportnumber="",passportexpirydate="",gender="",profile_pic="",kinname="",kinrelation="",kinaddress="",kinphonenumber="",kinemail = "")
if instance.user_type==3:
employee.objects.create(admin = instance,Dob="",Nationality="",Address="",Postcode="",Telephone="",Wage="",Passportnumber="",passportexpirydate="",gender="",profile_pic="",kinname="",kinrelation="",kinaddress="",kinphonenumber="",kinemail = "")
this is the function that is meant to save the inputs into the senioremployee's table and email,password , firstname, lastname,username into the custom user's table but currently it only stores email,password , firstname, lastname,username into the custom user's table and just ignores the other user inputs
def add_seniorEmployee_save(request):
if request.method!="POST":
return HttpResponse("Method Not Allowed")
else:
first_name=request.POST.get("first_name")
last_name=request.POST.get("last_name")
username=request.POST.get("username")
email=request.POST.get("email")
password=request.POST.get("password")
Dob=request.POST.get("Dob")
Nationality=request.POST.get("Nationality")
Address=request.POST.get("Address")
Postcode=request.POST.get("Postcode")
Telephone=request.POST.get("Telephone")
Wage=request.POST.get("Wage")
Passportnumber=request.POST.get("Passportnumber")
passportexpirydate=request.POST.get("passportexpirydate")
gender=request.POST.get("gender")
kinname=request.POST.get("kinname")
kinrelation=request.POST.get("kinrelation")
kinaddress=request.POST.get("kinaddress")
kinphonenumber=request.POST.get("kinphonenumber")
kinemail=request.POST.get("kinemail")
try:
user = CustomUser.objects.create_user(username=username,password=password,email=email,first_name=first_name,last_name=last_name,user_type=2)
user.senioremployee.Dob = Dob
user.senioremployee.Nationality = Nationality
user.senioremployee.Address = Address
user.senioremployee.Postcode = Postcode
user.senioremployee.Telephone = Telephone
user.senioremployee.Wage = Wage
user.senioremployee.Passportnumber = Passportnumber
user.senioremployee.passportexpirydate = passportexpirydate
user.senioremployee.gender = gender
user.senioremployee.profile_pic=""
user.senioremployee.kinname = kinname
user.senioremployee.kinrelation = kinrelation
user.senioremployee.kinaddress = kinaddress
user.senioremployee.kinphonenumber = kinphonenumber
user.senioremployee.kinemail = kinemail
user.save()
messages.success(request, "Successfully Added Senior Employee")
return HttpResponseRedirect("/add_seniorEmployee")
except:
messages.error(request, "Couldnt add senior employee")
return HttpResponseRedirect("/add_seniorEmployee")
First you have a problem with your senioremployee creation query. You are using Dob and passportexpirydate as DateField.
class senioremployee(models.Model):
Dob = models.DateField()
passportexpirydate = models.DateField()
But when you are creating senioremployee record you are passing empty string which eventually produce exception.
post_save signal
try:
senioremployee.objects.create(admin = instance,Dob="",Nationality="",Address="",Postcode="",Telephone="",Wage="",Passportnumber="",passportexpirydate="",gender="",profile_pic="",kinname="",kinrelation="",kinaddress="",kinphonenumber="",kinemail = "")
except Exception as e:
print("e)
It will return the exception
['“” value has an invalid date format. It must be in YYYY-MM-DD format.']
so pass YYYY-MM-DD date format to Dob and passportexpirydate then senioremployee will be created.
When you are updating record, you are saving user model again which again raise post_save signal and you will be creating duplicate senioremployee record which return UNIQUE constraint failed.
So you must use this line of code to update senioremployee record
user.senioremployee.save()
instead of
user.save()

Django - filtering on foreign key on third table

I'm trying to filter a table in Django based on the value of a particular field of a ForeignKey which is a filed of a third table ForignKey.
class A(models.Model):
user_type = models.CharField(blank=True, max_length=32)
description = models.CharField(blank=True,max_length=512)
class B(models.Model):
user = models.OneToOneField(User, on_delete=models.PROTECT)
name = models.CharField(max_length=32, blank=True)
surname = models.CharField(max_length=32, blank=True)
user_type = models.ForeignKey(A,on_delete=models.SET_NULL, null=True)
class C(models.Model):
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
user_profile = models.ForeignKey(B,on_delete=models.SET_NULL, null=True)
test = models.CharField(blank=False,max_length=512)
here is the query that I wish to make :
I want to query on the C and find the user_type_id on the B then filter user_type value on A
something like this (just for showing what I want):
models.C.objects.filter(test="test").filter(B__user_type_id__A__user_type = 1)
final result:
I want to get all of the data that test="test" in table C and user_type = 1 in table A
Since the user_type field on the A model is a CharField. So you can not filter on a number. You can however filter on a value of that user_type field:
C.objects.filter(test='test', b__user_type__user_type='my_user_type_value')
Or you can filter on the primary key of the A object:
C.objects.filter(test='test', b__user_type_id=1)

Django add field to query set

Hi i have 3 models in my django projects, when user send request i have his dealer_id i need return query set with info about material from Material table and for each row add discount_percent from last model where dealer_id = current dealer_id and row id. Please help me if you have answer.
models
class Material(models.Model):
id = models.AutoField(primary_key=True)
color = models.IntegerField()
name = models.CharField(max_length=100)
material_width = models.IntegerField()
price = models.BigIntegerField(default=0)
class Dealer(models.Model):
id = models.AutoField(primary_key=True)
dealer_name = models.CharField(max_length=100)
dealer_phone = models.CharField(max_length=13, unique=True)
dealer_email = models.CharField(max_length=150, null=False, unique=True)
dealer_firm_name = models.CharField(max_length=100, null=True)
dealer_address = models.CharField(max_length=100, null=True)
dealer_unp = models.CharField(max_length=9, null=True)
dealer_amount = models.FloatField(default=0.0)
user_id = models.BigIntegerField(unique=True)
class MaterialDealerPrice(models.Model):
id = models.AutoField(primary_key=True)
dealer_id = models.BigIntegerField(null=False)
material_id = models.BigIntegerField(null=False)
discount = models.FloatField(null=True, default=0.0)
This looks like a set of models that were automatically created by running inspectdb. You should always treat that output as a first draft; there is a lot that needs to be done manually to tidy it up.
Firstly, your MaterialDealerPrice model needs to have foreign keys to Dealer and Material:
class MaterialDealerPrice(models.Model):
dealer = models.ForeignKey('Dealer', null=False)
material = models.ForeignKey('Material', null=False)
discount = models.FloatField(null=True, default=0.0)
Secondly, you should recognise that this model is in fact the through table of a many-to-many relationship.
class Material(models.Model):
...
dealers = models.ManyToManyField('Dealer', through='MaterialDealerPrice')
Now, you should be able to follow these relationships in your query. Unfortunately your question is not clear enough to know what you actually want to do; you should give an example of the desired output.
Annotate can be used for this purpose. https://docs.djangoproject.com/en/1.11/topics/db/aggregation/

exporting data from a database to another _mysql_exceptions.Warning: Data truncated for column 'x' at row 1

Hi I have a project in PHP and I want develop the same with Django, for many reasons I decided to create a new database, so now I have to export all the data from the old one to the new one,for doing that I use the models I developed for Django, it worked until I stuck with this error:
_mysql_exceptions.Warning: Data truncated for column 'bloomberg' at row 1
this is the model of the table where I am experimenting this issue:
class Contact(models.Model):
company_id = models.ForeignKey(Company)
address = models.CharField(max_length=150)
first_name= models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
role = models.CharField(max_length=20)
sector = models.CharField(max_length=45)
work_phone = models.CharField(max_length=30)
contact_source = models.CharField(max_length=30)
alt_work_phone = models.CharField(max_length=30)
mobile_phone = models.CharField(max_length=30)
work_fax = models.CharField(max_length=30)
bloomberg = models.CharField(max_length=60)
work_email = models.CharField(max_length=60)
research_email = models.CharField(max_length=60)
product_focus = models.CharField(max_length=2)
preferred_email = models.CharField(max_length=60)
job_title = models.CharField(max_length=80)
created_by = models.CharField(max_length=25)
legal_entity_name = models.CharField(max_length=100)
status= models.ForeignKey(Status)
title = models.CharField(max_length=5)
zipcode = models.CharField(max_length=10)
country = models.CharField(max_length=15)
city= models.CharField(max_length=20)
created_date=models.DateTimeField('creation date ')
updated_date=models.DateTimeField('update date ')
updated_by = models.CharField(max_length=20)
parent = models.CharField(max_length=45)
address_line_2 = models.CharField(max_length=100)
new = models.BooleanField()
hided = models.BooleanField()
employee = models.BooleanField()
def __unicode__(self):
s = u" Contact "
return s + self.first_name + " " + self.last_name
the fields in both databases have the same length,so I do not understand the error, after googling I noticed that usually this problem is solved fixing the dimensions of the column, but this is not my case. can somebody tell me how to fix it?
it looks like 60 Chars are not enough for your "bloombergs". Try to set this higher like:
bloomberg = models.CharField(max_length=255)
Notice, that you will also have to do this on database level if you already synced your models. Hope this helps.