Django and MySQL -- the data is different between the two - mysql

I have a Django app that uses MySQL as a backend. I'm having difficulties where the raw MySQL records show one value, but Django presents something else in the web app.
For example, I have a table for client data. One of the fields in each record is called snailMailInvoice and is a Y/N choice -- default is Y (varchar type).
+-------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+----------------+
| snailMailInvoice | varchar(3) | NO | | Y | |
The raw MySQL:
select * from systems_system_contact where lastName="SomeClient";
...a bunch of other fields... | snailMailInvoice |
...a bunch of other fields... | N
Then, in the Django App, the form displays Y (the other choice). It is like the Django App can't see the MySQL value, so it defaults to Y. If, through the Django App, I select N and save the form, THEN the value sticks to N in Django.
Why would this be happening?
EDIT - to add some code
Forms.py:
class System_Contact_Form(ModelForm):
class Meta:
model = System_Contact
exclude = ('isMainContact', 'systemOwner', 'companyName', 'isRessyContact')
Views.py:
def contact_details(request, scID):
redirect_to = request.REQUEST.get('next', '/systems/contacts/')
if request.method == 'POST':
syscontEdit = System_Contact.objects.get(pk=scID)
form = System_Contact_Form(request.POST, instance=syscontEdit)
if form.is_valid():
form.save()
return HttpResponseRedirect(redirect_to)
else:
syscontView = System_Contact.objects.get(pk=scID)
form = System_Contact_Form(instance=syscontView)
c = {
'form':form,
'cancel':redirect_to
}
return render_to_response('pages/systems/contact_details.html', c, context_instance=RequestContext(request))
Models.py:
class System_Contact(models.Model):
IS_MAIN_CONTACT_CHOICES = (
('Y', 'Yes'),
('N', 'No'),
)
IS_SYSTEM_OWNER_CHOICES = (
('Y', 'Yes'),
('N', 'No'),
)
IS_RESSY_CONTACT_CHOICES = (
('Y', 'Yes'),
('N', 'No, this is a commercial contact'),
)
TRADE_CHOICES = (
('EL', 'Electrician'),
('LA', 'Landscaper'),
('PL', 'Plumber'),
('TR', 'Trencher'),
)
SNAIL_MAIL_CHOICES = (
('Y', 'Yes'),
('N', 'No'),
)
SNAIL_MAIL_INVOICE_CHOICES = (
('Y', 'Yes'),
('N', 'No'),
)
firstInitial = models.CharField(max_length = 10, verbose_name = 'First Initial', blank = True, null = True)
firstName = models.CharField(max_length = 60, verbose_name = 'First Name', blank = True, null = True)
lastName = models.CharField(max_length = 160, verbose_name = 'Last Name', blank = True, null = True)
phonetically = models.CharField(max_length = 100, verbose_name = 'Phonetically', blank = True, null = True)
companyName = models.CharField (max_length = 160, verbose_name = 'Company Name', blank = True, null = True) #Only used for Commercial Owners, no other field needed
homePhone = models.CharField(max_length = 60, verbose_name = 'Home Phone Number', blank = True, null = True)
officePhone = models.CharField(max_length = 60, verbose_name = 'Office Phone Number', blank = True, null = True)
cellPhone = models.CharField(max_length = 60, verbose_name = 'Cell Phone Number', blank = True, null = True)
faxNumber = models.CharField (max_length= 60, blank=True, null=True, verbose_name = 'Fax Number')
isMainContact = models.CharField (max_length = 3, verbose_name = 'Is the Main Contact?', choices = IS_MAIN_CONTACT_CHOICES, default='N')
isRessyContact = models.CharField (max_length = 3, verbose_name = 'Is this a Ressy Contact?', choices = IS_RESSY_CONTACT_CHOICES, default='Y')
isArchived = models.BooleanField(verbose_name = 'Archived?', default = False)
systemOwner = models.CharField (max_length = 3, verbose_name = 'Is a System Owner?', choices = IS_SYSTEM_OWNER_CHOICES, default='N') #this is just a flag to say they own a system
worksFor = models.CharField (max_length = 70, verbose_name = 'Works For', blank = True, null = True)
tradeType = models.ForeignKey(Contact_Trade, blank=True, null=True, verbose_name='Trade')
emailAddress = models.EmailField(verbose_name = 'Email Address', blank = True, null = True)
billingAddress = models.CharField(max_length = 150, verbose_name = 'Billing Address', blank=True, null=True )
billingCity = models.CharField(max_length = 90, verbose_name = 'Billing City', blank=True, null=True)
billingProvince = models.CharField(max_length = 30, verbose_name = 'Billing Province', blank=True, null=True)
billingPostalCode = models.CharField(max_length = 10, verbose_name = 'Billing Postal Code', blank=True, null=True)
snailMailOnly = models.CharField(max_length = 3, verbose_name = 'Snail Mail Only?', choices = SNAIL_MAIL_CHOICES, default='Y')
snailMailInvoice = models.CharField(max_length = 3, verbose_name = 'Snail Mail Invoice?', choices = SNAIL_MAIL_INVOICE_CHOICES, default='Y')

OK -- I figured it out.
I realised there was something connected with the fact that in my CSV file, the snailMailInvoice field is the LAST field on each line. Thus, at the end of the line there is a carriage return. I assumed that this was a \n -- thus in my MySQL command to import the CSV, I state terminated by '\n'.
However, the MySQL picked up a '\r' on EVERY line and was adding it to the snailMailInvoice field. Thus, every record has either a Y or a N with a \r attached.
I amended my MySQL import statement to have: lines terminated with '\r\n' Now everything is working as expected.
Lesson learned.
Thanks for the help.

Related

What is best way to add product features in django with database mysql

I want to store product features in MySQL database so, I've some confusion about which way is better to store it for better performance as well as for scalability.
Basically as per my idea, I design models like given below: in that, I have a product table, features table which contains field product type for which type of product belongs to this particular feature. the last table is product_features in that I actually storing data of specific product_id and feature_id is it the right way?
I'm using Django. so here's is my model!
class Product(models.Model):
product_id = models.AutoField(primary_key=True)
product_name = models.CharField(max_length=255, null = False, unique=True)
product_slug = models.SlugField(unique=True, blank=True, null=True)
product_title = models.CharField(max_length=255, null = True)
product_info = models.TextField(null = False)
product_description = models.CharField(max_length = 255)
product_price = models.CharField(max_length = 255)
brand = models.ForeignKey(Brand, on_delete=models.SET_NULL, null=True)
product_type = models.ForeignKey(Product_type, on_delete=models.SET_NULL, null=True)
product_status = models.CharField(max_length = 100, default = "publish")
created_at = models.DateTimeField(auto_now_add = True)
updated_at = models.DateTimeField(auto_now = True)
class Feature(models.Model):
feature_id = models.AutoField(primary_key=True)
feature_name = models.CharField(max_length=255, null = False)
product_type = models.ForeignKey(Product_type, on_delete=models.SET_NULL, null=True)
feature_has_value = models.CharField(max_length = 1, null = False)
created_at = models.DateTimeField(auto_now_add = True)
updated_at = models.DateTimeField(auto_now = True)
class Product_feature(models.Model):
product_feature_id = models.AutoField(primary_key=True)
feature = models.ForeignKey(Feature, on_delete=models.SET_NULL, null=True)
product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True)
product_feature_value = models.CharField(max_length=255, null = False)
created_at = models.DateTimeField(auto_now_add = True)
updated_at = models.DateTimeField(auto_now = True)
As per my thought your model is looks good for the product feature except primary key field in all model, no need to manage primary key in django model because django already manage id field as primary key for each model.
Second thing is generally many time there will be discounts on particular product in online shopping site so, if your plan is also something like that then you can also add one discount model in your app.

Django - remove primary_key=True from an existing model?

I have a table that I've already migrated into the MySQL database and one of the fields has primary_key = True. I realized this needs to be different and I need to remove the primary_key = True. I'd like Django to create its own auto incrementing primary key fields (like it does with all models if you don't specify a primary_key).
If I simply remove the primary_key = True, Django complains because it can't auto-assign an auto-increment value to its new id field (which is now the primary key field).
What's the best way to change this one field to not have primary_key = True? This particular model doesn't have any records yet in the database, so I think I'm in a better position than if I had records in there. I'm not sure if I should just drop the table and migrate as if it's a brand new table or if I need to take some other approach?
Edit
What I actually tried:
python manage makemigrations accounting
The model in question is called Invoice and I'm wanting to change the field inv_num to not be the primary key
Django asks:
You are trying to add a non-nullable field 'id' to invoice without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows)
2) Quit, and let me add a default in models.py
Select an option:
So I select option 1, and type 1.
Django creates the migration file, so I do:
python manage migrate accounting
And Django complains with:
django.db.utils.OperationalError: (1067, "Invalid default value for 'id'")
My research indicates that this is because you can't auto-assign a value to a primary key field.
Edit 2
The Model in question:
class Invoice(models.Model):
inv_num = models.CharField(max_length = 144, primary_key = True, verbose_name = 'Invoice Number')
brokerage = models.ForeignKey(Brokerage, blank = True, null = True, verbose_name = 'Brokerage')
lot = models.ForeignKey(Lot, on_delete = models.SET_NULL, blank = True, null = True, verbose_name = "Lot")
vendor = models.ForeignKey(Vendor, on_delete = models.SET_NULL, blank = True, null = True, verbose_name = 'Bill To (Vendor)')
contact = models.ForeignKey(Contact, on_delete = models.SET_NULL, blank = True, null = True, verbose_name = 'Bill To (Contact)')
due_date = models.DateField(blank = True, null = True, verbose_name = 'Date')
fund = models.ForeignKey(Fund, on_delete = models.SET_NULL, blank = True, null = True)
org_amt = models.DecimalField(max_digits = 12, decimal_places = 2, default = 0, verbose_name = 'Original Amount of Invoice')
amtos = models.DecimalField(max_digits = 12, decimal_places = 2, default = 0, verbose_name = 'Amount OS')
is_fine = models.BooleanField(default = False)
is_reversed = models.BooleanField(default = False, verbose_name = 'Has Been Reversed')
is_paid = models.BooleanField(default = False, verbose_name = 'Is Paid')
is_locked = models.BooleanField(default = False, verbose_name = 'Is Locked')
is_archived = models.BooleanField(default = False, verbose_name = 'Is Archvied')
org_je = models.CharField(max_length = 144, blank = True, null =True, verbose_name = 'Original JE')
There are a number of other models in this Django App that have Invoice as a foreign key. But those too don't have any data in them in the database.
I wonder if I should just drop the whole app from the database, and re-migrate it in? I do have a bunch of other apps that have data I cannot simply drop though.

Model in django to store working hours of an employee for specific year, month and week

I am creating a Django application for online attendance system that has to store weekly working hours of employees for few years(5 or 6)
I want to create a table, that has employeeid, year, month, week and hours(example 4hrs for week1 in Jan 2015 for employee with id 200)
I tried creating models(year, month, week, record) with few ManyToManyFields and Foreignkey Fields, but ended up in creating tables that link years and employeeid, employeeid and month, years and month but not a table with the fields employeeid, week, month and year.
Could anyone tell me the relations between the fields to be used in models to create a database for this.
Thanx in advance
Here is my Models.py -
from django.db import models
class Month(models.Model):
id = models.IntegerField(primary_key = True, null = False)
weeks = models.ManyToManyField(Week, null = True)
class Year(models.Model):
id = models.IntegerField(primary_key = True, null = False)
month = models.ManyToManyField(Month, null = True)
class UserId(models.Model):
id = models.IntegerField(primary_key = True, null = False)
name = models.CharField(max_length = 56) // employee name
year = models.ForeignKey(Year, null = True)
class Week(models.Model):
id = models.IntegerField(primary_key =True, null = False)
working_hours = models.IntegerField(null = True, default = 0)
class Record(models.Model):
id = models.IntegerField(primary_key = True, null = False)
month = models.ManyToManyField(Month, null = True)
year = models.ManyToManyField(Year, null = True)
week = models.ManyToManyField(Week, null=True)
userid = models.ForeignKey(UserId, null = True)
This model will satisfy your requirements
Note: you don't have to create primary key(id) field, it will be automatically created by django.
from django.db import models
class Employee(models.Model):
name = models.CharField(max_length = 56)
class Record(models.Model):
employee = models.ForeignKey(Employee)
month = models.IntegerField()
year = models.IntegerField()
week = models.IntegerField()
hours = models.IntegerField()
class Meta:
unique_together = (('employee', 'month', 'year', 'week'),)

Why 2 queries are executed instead of one?

I have following piece of code:
def detail(request, popular_id):
try:
popular = Popular.objects.get(pk = popular_id)
share = Share.objects.get(isin = popular.isin) #LINE 1
chart_data_json = share.get_chart_data_json()
except Popular.DoesNotExist:
raise Http404
return render(request, 'popular/detail.html', {'popular': popular, 'chart_data': chart_data_json})
In LINE 1 I noticed using debug-toolbar that there are two queries get executed:
SELECT `share_share`.`id`, `share_share`.`symbol`, `share_share`.`isin`, `share_share`.`name`, `share_share`.`market`, `share_share`.`updated` FROM `share_share` WHERE `share_share`.`id` = 1
and
SELECT `share_share`.`id`, `share_share`.`symbol`, `share_share`.`isin`, `share_share`.`name`, `share_share`.`market`, `share_share`.`updated` FROM `share_share` WHERE `share_share`.`isin` = 'US5949181045'
I cannot understand why we need the first query and how to avoid it?
EDIT:
Model definition of share:
class Share(models.Model):
symbol = models.CharField(max_length = 32)
isin = models.CharField(max_length = 12)
name = models.CharField(max_length = 256)
market = models.CharField(max_length = 64)
updated = models.BooleanField(default = False)
def get_chart_data_json(self):
quote_model = create_quote_model(str(self.isin))
data = quote_model.objects.values('date', 'adj_close', 'volume')
chart_data = []
for d in data.iterator():
chart_data.append({'date': d['date'].isoformat(), 'value': d['adj_close'], 'volume': d['volume']})
chart_data_json = json.dumps(chart_data)
return chart_data_json
def __unicode__(self):
return self.isin
Model definition of popular:
class Popular(models.Model):
title = models.CharField(max_length = 120)
text = models.CharField(max_length = 1024)
isin = models.ForeignKey(Share)
def __unicode__(self):
return self.title
First query is evaluated when you access foreign key isin from popular object:
share = Share.objects.get(isin = popular.isin)
Second query gets Share object:
share = Share.objects.get(isin = popular.isin)
If you want just one query at #LINE 1 you should replace it with:
share = popular.isin #LINE 1

edit and save the already existing data from database in django

models.py is
class Book(models.Model):
book_name=models.CharField(max_length=30)
author_name=models.CharField(max_length=30)
publisher_name=models.CharField(max_length=40)
author=models.ForeignKey(Author)
def __unicode__(self):
..........
class Author(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email = models.EmailField()
age=models.IntegerField()
def __unicode__(self):
........
def books(self):
return Book.objects.filter(author=self)
I need to perform edit and save the already existing data in a table from database.I am using 2 models.since i did for doing the same by single table,have some confusion to perform with two table.I am a learner of django.plz help me with this
plz check my views.py for edit option.
def editbook(request,book_id):
if request.POST:
book_name =request.POST['book_name']
publisher_name =request.POST['publisher_name']
books=Book.objects.filter(book_id=book_id).update(book_name=book_name, publisher_name=publisher_name)
first_name = request.POST.get('first_name')
last_name = request.POST.get('last_name')
email = request.POST.get('email')
age = request.POST.get('age')
author = Author.objects.update(first_name = first_name,last_name = last_name,email=email,age=age)
return redirect('/index/')
else:
books = Book.objects.get(pk=book_id)
return render_to_response('editbook.html',{'books':books},context_instance=RequestContext(request))
ya.this is not working properlly.plz guide me how to perform that.
def editbook(request,book_id):
books = Book.objects.get(pk=book_id)
if request.POST:
book_name = request.POST.get('book_name')
publisher_name = request.POST.get('publisher_name')
books.book_name = book_name
books.publisher = publisher_name
get_author = books.save()
first_name = request.POST.get('first_name')
last_name = request.POST.get('last_name')
email = request.POST.get('email')
age = int(request.POST.get('age'))
author = Author.objects.get(pk=get_author.author_id)
author.first_name = first_name
author.last_name = last_name
author.email = email
author.age = age
author.save()
return redirect('/index/')
return render_to_response('editbook.html',{
'books':books
},context_instance=RequestContext(request))