Model's fields being cleared after ModelForm.save() called - django

I'm having an issue where I instantiate a ModelForm-based form with a pre-existing instance of the model in the GET portion of my view function. This model already has several fields filled in; the ModelForm is used to collect other fields in the model. The pre-existing fields are excluded in the ModelForm's definition.
The problem is, during POST processing, after successful validation of the ModelForm, I'm calling ModelForm.save(commit=False)...and the model returned (which should be the same one I passed in as 'instance' in the GET handling, remember) has somehow lost all the fields that were previously set. The fields actually set by the form are fine; but it's no longer the original instance of my model.
This is not the behavior I expected; and in fact I've used this partial-model-in-modelform previously, and it works other places. What am I missing here??
Hopefully some code'll make all this clear...
So here's the Model:
class Order(models.Model):
STATUS = (
('Created', -1),
('Pending', 0),
('Charged', 1),
('Credited', 2),
)
SHIPPING_STATUS = (
('Cancelled', 0),
('Ready for pickup', 1),
('Shipped', 2),
('OTC', 3),
)
orderID = models.IntegerField(max_length=15, null=True, blank=True)
store = models.ForeignKey(Store)
paymentInstrument = models.ForeignKey(PaymentInstrument, null=True, blank=True)
shippingMethod = models.ForeignKey(ShippingMethod, null=True, blank=True)
last_modified = models.DateTimeField(null=True, blank=True)
date = models.DateTimeField(auto_now_add=True, null=True, blank=True)
total = models.FloatField(default=0.0, blank=True)
shippingCharge = models.FloatField(default=0.0, blank=True)
tax = models.FloatField(default=0.0, blank=True)
status = models.CharField(max_length=50, choices=STATUS, default = 'Created')
shippingStatus = models.CharField(max_length=50, choices=SHIPPING_STATUS, default = '1')
errstr = models.CharField(max_length=100, null=True, blank=True)
# billing info
billingFirstname = models.CharField(max_length = 50, blank = True)
billingLastname = models.CharField(max_length = 50, blank = True)
billingStreet_line1 = models.CharField(max_length = 100, blank = True)
billingStreet_line2 = models.CharField(max_length = 100, blank = True)
billingZipcode = models.CharField(max_length = 5, blank = True)
billingCity = models.CharField(max_length = 100, blank = True)
billingState = models.CharField(max_length = 100, blank = True)
billingCountry = models.CharField(max_length = 100, blank = True)
email = models.EmailField(max_length=100, blank = True)
phone = models.CharField(max_length=20, default='', null=True, blank=True)
shipToBillingAddress = models.BooleanField(default=False)
# shipping info
shippingFirstname = models.CharField(max_length = 50, blank = True)
shippingLastname = models.CharField(max_length = 50, blank = True)
shippingStreet_line1 = models.CharField(max_length = 100, blank = True)
shippingStreet_line2 = models.CharField(max_length = 100, blank = True)
shippingZipcode = models.CharField(max_length = 5, blank = True)
shippingCity = models.CharField(max_length = 100, blank = True)
shippingState = models.CharField(max_length = 100, blank = True)
shippingCountry = models.CharField(max_length = 100, blank = True)
Here's the ModelForm definition:
class OrderForm(ModelForm):
class Meta:
model = Order
exclude = ('orderID',
'store',
'shippingMethod',
'shippingStatus',
'paymentInstrument',
'last_modified',
'date',
'total',
'payportCharge',
'errstr',
'status', )
widgets = {
'billingCountry': Select(choices = COUNTRIES, attrs = {'size': "1"}),
'shippingCountry': Select(choices = COUNTRIES, attrs = {'size': "1"}),
'billingState': Select(choices = STATES, attrs = {'size': "1"}),
'shippingState': Select(choices = STATES, attrs = {'size': "1"}),
}
And here is the view function:
def checkout(request):
theDict = {}
store = request.session['currentStore']
cart = request.session.get('cart', False)
order = request.session['currentOrder'] # some fields already set
if not cart: # ...then we don't belong on this page.
return HttpResponseRedirect('/%s' % store.urlPrefix)
if request.method == 'GET':
form = OrderForm(instance=order, prefix='orderForm')
else: # request.method == 'POST':
logging.info("Processing POST data...")
form = OrderForm(request.POST, prefix='orderForm')
if form.is_valid():
### AT THIS POINT, ORDER FIELDS ARE STILL GOOD (I.E. FILLED IN)
order = form.save(commit=False)
### AFTER THE SAVE, WE'VE LOST PRE-EXISTING FIELDS; ONLY ONES SET ARE
### THOSE FILLED IN BY THE FORM.
chargeDict = store.calculateCharge(order, cart)
request.session['currentOrder'] = order
return HttpResponseRedirect('/%s/payment' % store.urlPrefix)
else:
logging.info("Form is NOT valid; errors:")
logging.info(form._errors)
messages.error(request, form._errors)
theDict['form'] = form
theDict['productMenu'] = buildCategoryList(store)
t = loader.get_template('checkout.html')
c = RequestContext(request, theDict)
return HttpResponse(t.render(c))
Any/all help appreciated...

When you instantiate the form during the POST, the instance of the model you're editing is None, because you're not passing it in, and you're not persisting the form instance from the GET. Django won't do anything on its own to persist data between requests for you.
Try:
...
form = OrderForm(request.POST or None, instance=order, prefix='orderForm')
if request.method == 'POST':
logging.info("Processing POST data...")
if form.is_valid():
...
Now the instance will be populated for GET and POST, but the data from request.POST is optional for the form if it's None. It also saves you from having to instantiate the form in two places depending on request.method

Related

Show all products from category parent

im new in django, and im doing an ecommerce website. I have a problem, When I click on a subcategory its okay, it shows all the products of that subcategory. But I want to click on a category parent and show all the products that his children has, and i dont know how to do that.
Here is my models:
class Category(models.Model):
parent = models.ForeignKey('self', related_name='children', on_delete=models.CASCADE, blank = True, null = True)
title = models.CharField(max_length= 200, null = True)
slug = models.SlugField(max_length=200, null = True)
ordering = models.IntegerField(default = 0)
class Meta:
verbose_name_plural = 'Categories'
ordering = ('ordering',)
def __str__(self):
return self.title
class Product(models.Model):
name = models.CharField(max_length = 255, null = True)
slug = models.SlugField(max_length=200)
category = models.ForeignKey(Category, related_name='products', on_delete = models.CASCADE)
parent = models.ForeignKey('self', related_name = 'variants', on_delete = models.CASCADE, blank = True, null = True)
brand = models.ForeignKey(Brand, related_name='products', null = True, on_delete = models.CASCADE)
description = models.TextField(blank = True, null = True)
price = models.FloatField(null = True)
disccount = models.BooleanField(default = False)
disccount_price = models.FloatField(blank = True, null = True)
image = models.ImageField(upload_to = 'images/',blank = True, null = True)
thumbnail = models.ImageField(upload_to = 'images/', blank = True, null = True)
date_added = models.DateTimeField(auto_now_add = True)
class Meta:
ordering = ('-date_added',)
def __str__(self):
return self.name
Here is my view:
def category_detail(request, slug):
products = Product.objects.all()
subcategories = []
if slug:
category = get_object_or_404(Category, slug=slug)
products = products.filter(category = category)
subcategories = Category.objects.filter(parent = category)
context = {
'category': category,
'subcategories': subcategories,
'products' : products,
}
return render(request, 'category_detail.html', context)
So please I need some help:(
You can filter on Products that belong to a subcategory of a category category with:
products = products.filter(category__parent=category)
or if you want Products that belong to the category or to a category with as parent the category, you can filter with Q objects [Django-doc]:
from django.db.models import Q
products = products.filter(
Q(category__parent=category) |
Q(category=category)
)

Django Testing IntegrityError: duplicate key value violates unique constraint DETAIL: Key (project_id)=(1023044) already exists

I have not been able to resolve this IntegrityError issue in my Django's unittest. Here are my models:
class UserProfile(models.Model):
''' UserProfile to separate authentication and profile '''
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete = models.CASCADE, null = True, blank = True)
# Note: first name and last name is in the custom User model
first_name = models.CharField(max_length = 20, blank = True, null = True)
last_name = models.CharField(max_length = 30, blank = True, null = True)
address = models.CharField(max_length = 100, null=True, blank = True)
address_city = models.CharField(max_length = 30, null = True, blank = True)
metropolitan = models.CharField(max_length = 30, null = True, blank = False)
class Municipality(models.Model):
name = models.CharField(max_length = 50)
abb = models.CharField(max_length = 5)
date_created = models.DateTimeField(auto_now_add = True)
date_modified = models.DateTimeField(auto_now = True)
userprofile = models.ForeignKey('user_profile.UserProfile', blank = False, null = False, related_name = 'userprofile_municipalities', on_delete = models.CASCADE)
class Project(models.Model):
name = models.CharField(max_length = 50)
logo = models.ImageField(null=True, blank = True, width_field = 'logo_width', height_field = 'logo_height')
logo_height = models.IntegerField(default = 40)
logo_width = models.IntegerField(default = 40)
date_created = models.DateTimeField(auto_now_add = True )
date_modified = models.DateTimeField(auto_now = True )
# RELATIONSHIPS:
user_profiles = models.ManyToManyField('user_profile.UserProfile', through = 'ProjectAssociation', through_fields = ('project', 'user_profile' ), blank = True, related_name = 'user_projects')
address = models.OneToOneField(Address, on_delete = models.PROTECT, null = True, blank = True)
municipality = models.ForeignKey('development.Municipality', related_name = 'municipality_projects', null = False, blank = False)
class Job(models.Model):
project = models.OneToOneField('project_profile.Project', blank = False, on_delete = models.CASCADE, related_name = 'job')
...
date_modified = models.DateTimeField(auto_now_add = True)
date_created = models.DateTimeField(auto_now = True)
class Invoice(models.Model):
PO = models.CharField(max_length = 50, blank = False, null = False) # e.g. Mixed Use Residential Commercial Rental Invoice
invoice_type = models.CharField(max_length = 40)
date_created = models.DateTimeField(auto_now = True)
date_modified = models.DateTimeField(auto_now_add = True)
job = models.ForeignKey(DevelopmentProject, related_name = 'job_invoices', blank = True, null = True, on_delete = models.CASCADE)
Invoice_creator = models.ForeignKey('user_profile.UserProfile', related_name = 'created_invoices', blank = True, null = True, on_delete = models.SET_NULL) # ModelForm to enforce If the Invoice creator's account is closed, the corresponding Invoices to be preserved
Invoice_reviewer = models.ForeignKey('user_profile.UserProfile', related_name = 'reviewed_invoices', blank = True, null = True , on_delete = models.SET_NULL ) # The reviewer is not necessary, but
...
In my unittest, I am getting integrity error message even when I try to explicitly assign unique id to the created instance:
class UpdateinvoiceTestCase(TestCase):
''' Unit Test for Updateinvoice View '''
def setUp(self):
self.factory = RequestFactory()
# Create the dependencies
self.userprofile = mommy.make('user_profile.UserProfile')
print ('User profile: ', self.userprofile, ' - userprofile id: ', self.userprofile.id )
self.municipality = mommy.make('development.municipality', userprofile = self.userprofile, _quantity=1)
self.project = mommy.make('project_profile.Project', municipality = self.municipality[0], _quantity=2)
self.job = mommy.make('development.Job', project = self.project[0] )
# Create invoice
self.invoice = mommy.make('development.invoice', job = self.job)
# Passing the pk to create the url
the_uri = reverse('development:update_invoice', args=(self.invoice.pk,))
the_url = 'http://localhost:8000' + reverse('development:update_invoice', args=(self.invoice.pk,))
# Creating a client:
self.response = self.client.get(the_url, follow=True)
def test_url(self):
''' Ensure that the url works '''
self.assertEqual(self.response.status_code, 200)
I have made sure only one test is run using so there is no sharing of the data between different testcases that would throw Django off:
python manage.py test project.tests.test_views.UpdateViewTestCase
I get the the following error message:
IntegrityError: duplicate key value violates unique constraint "development_developmentproject_project_id_key"
DETAIL: Key (project_id)=(1) already exists
I also tried using mommy.make to create project, but I got the same error message. I also tried to specifically assign non-existent ids to the Project creation line, but could not convince Django to stop complaining.
So, Project is being created twice, but I cannot figure out why and where. Any help is much appreciated!
It turned out that I've used signals which created an instance already and I was creating the same instance in my setUpTestData again. The solution was to avoid creating a duplicate instance or simply use get_or_create instead of create or mommy.make

Django Restframework “Got AttributeError when attempting to get a value for field `section_type` on serializer `QuestionSerializer`

This is my model.
class QuestionSection(models.Model):
section = models.CharField(max_length = 100, null = True, blank = True)
max_marks = models.IntegerField()
def __str__(self):
return self.section
class Question(models.Model):
question_section = models.ForeignKey(QuestionSection, on_delete = models.CASCADE, related_name = 'questions')
section_type = models.CharField(max_length = 10,)
questions = models.CharField(max_length = 350, null = True, blank = True)
image = models.CharField(max_length = 10, null = True, blank = True)
no_of_images = models.IntegerField(null = True, blank = True)
marks = models.IntegerField()
shop_view = models.CharField(max_length = 30, null = True, blank = True, choices=(('critical', 'critical'), ('major', 'major')))
what_to_look_for = models.CharField(max_length = 350, null = True, blank = True)
def __str__(self):
return "{}-{}".format(self.section_type, self.marks)
This is my serializer
class QuestionSerializer(serializers.ModelSerializer):
class Meta:
model = Question
fields = '__all__'
class QuestionSectionSerializer(serializers.ModelSerializer):
questions = QuestionSerializer(read_only = True)
class Meta:
model = QuestionSection
fields = '__all__'
I am not able to figuring out how to do it. The API will be like QuestionSection fields and inside that Question fields will be there.

why would my databse entries be coming up empty?

I'm trying to create new instances based off of the form below however when it saves, the database only saves the foreign key value and the order value but nothing else. Any Idea why?
models:
class danceType(models.Model):
eventtype = models.ForeignKey(compType)
dance_name = models.CharField(max_length = 20)
Price = models.FloatField(max_length = 7, null=True, blank=True )
field_2 = models.CharField(max_length = 20, null=True, blank=True)
def __unicode__(self):
fields = ('field_2')
return self.dance_name
class dancer(models.Model):
dancetype = models.ForeignKey(danceType)
e_number = models.IntegerField(max_length = 4, null=True, blank=True)
D1_first_name = models.CharField(max_length = 20)
D1_last_name = models.CharField(max_length = 20)
D1_email = models.EmailField(max_length = 20)
city = models.CharField(max_length = 20)
order = models.PositiveIntegerField()
perform = models.NullBooleanField(null=True, blank=True)
View:
if request.method =='POST':
sub_type = request.POST.get("form")
dancetype = request.POST.get("dancetype")
ext = request.POST.get("quantity")
form = modelformset_factory(dancer, form=CompReg, extra=int(ext))
productform = form(queryset = dancer.objects.none())
if sub_type == "submitted":
productform = form(request.POST, request.FILES)
if productform.is_valid():
for p in productform:
dance=p.save(commit=False)
dance.dancetype_id = 4
dance.order = 1
dance.save()
else:
der = "it didnt save"

'RelatedManager' object is not iterable Django

Hey i have looked around through some simliar posts here on SO but havent found anything that has solved my problem. I have the following models,
from django.db import models
class Areas(models.Model):
name = models.CharField(max_length = 120)
order_in_sidebar_network = models.IntegerField(blank=True, null=True)
order_in_section_network = models.IntegerField(blank=True, null=True)
def __unicode__ (self):
return self.area_name
class Meta:
verbose_name_plural = "Areas"
verbose_name = "Area"
class Countries(models.Model):
name = models.CharField(max_length = 120, help_text = "The name of the country")
area = models.ForeignKey(Areas, verbose_name = 'Area')
def __unicode__ (self):
return self.name
class Meta:
verbose_name_plural = "Countries"
verbose_name = "Country"
ordering = ['name']
class Offices(models.Model):
country = models.ForeignKey(Countries, verbose_name = 'Country')
name = models.CharField(max_length = 255, help_text = "The name of this office, IE London")
main_office = models.BooleanField(default= False, help_text = "Is this office a key location?", verbose_name = "Key Location")
address_1 = models.CharField(max_length = 255, null = True, blank = True)
address_2 = models.CharField(max_length = 255, null = True, blank = True)
address_3 = models.CharField(max_length = 255, null = True, blank = True)
city = models.CharField(max_length = 255, null = True, blank = True)
postcode = models.CharField(max_length = 20)
tel = models.CharField(max_length = 30, null= True, blank = True, help_text = "Optional telephone contact number")
mobile = models.CharField(max_length = 30, null= True, blank = True, help_text = "Optional mobile contact number")
fax = models.CharField(max_length = 30, null= True, blank = True, help_text = "Optional fax contact number")
data_1 = models.CharField(max_length = 255, null = True, blank = True, help_text = "Optional additional data", verbose_name = "Additional information")
data_2 = models.CharField(max_length = 255, null = True, blank = True, help_text = "Optional additional data", verbose_name = "Additional information")
class Meta:
verbose_name_plural = "Offices"
verbose_name = "Office"
ordering = ['name']
def __unicode__(self):
return self.name
class OfficeMembers(models.Model):
name = models.CharField(max_length = 60, help_text = "Please tell us this person name")
title = models.CharField(max_length = 100, help_text = "The person's title, IE Managing Director")
email = models.EmailField(max_length = 255, null = True, blank = True, help_text = "Optional email address for this person")
email2 = models.EmailField(max_length = 255, null = True, blank = True, help_text = "Optional second email address for this person")
phone = models.CharField(max_length = 30, null = True, blank = True, help_text = "Optional contact number for this person")
mobile = models.CharField(max_length = 30, null = True, blank = True, help_text = "Optional mobile contact number for this person")
office = models.ForeignKey(Offices, null = True)
class Meta:
verbose_name_plural = "Office Memebers"
verbose_name = "Office memebr"
ordering = ['name']
def __unicode__(self):
return self.name
i have the following view set up
def index(request):
cache_key = "world_areas"
cache_time = 60
world_areas_cache = cache.get(cache_key)
#if no cache is set, grab the objects, and set the cache
logger.debug(world_areas)
if not world_areas_cache:
logger.info('No cache found grabbing objects')
world_areas = Areas.objects.select_related().all()
#cache.set(cache_key, world_areas, cache_time)
logger.debug(world_areas)
else:
logger.info("Getting from cache")
world_areas = world_areas_cache
return render_to_response('network/index.html', {'world_areas':world_areas}, context_instance=RequestContext(request))
trying to iterate over the world_areas object like so
{% for area in world_areas %}
produces a template syntax error
'RelatedManager' object is not iterable
Any one got any ideas why this is happeing? really cant seem to get round this! strangley this is working for me in shell :S am i missing something obvious???
Big thanks to anyone able to help!
Call all() to retrieve the elements from the manager.
{% for area in world_areas.all %}
In general, it is better practice to use a values or values_list to pass data from a queryset to a template.
world_areas = Areas.objects.select_related().all().values_list('name', 'order_in_sidebar_network', ...)
Check out the Django docs for info on how to use the values function if you haven't used it before.
I run into this issue by a reckless mistake:
for physicalserver in task.physicalservers:
physicalserver.relieve_task()
The task.physicalservers is RelatedManager object, in my case I should get the task's physicalservers, there should add .all().
for physicalserver in task.physicalservers.all():
You have to iterate over set to get values related to a fk in a model.
class Area(models.Model):
name = models.CharField(max_length = 120)
order_in_sidebar_network = models.IntegerField(blank=True, null=True)
order_in_section_network = models.IntegerField(blank=True, null=True)
class CountryArea(models.Model):
name = models.CharField(max_length = 120)
area = models.ForeignKey(Area, verbose_name = 'Area')
## Assuming you have an object; area
## To get the all the counties related to that area you can do;
{% for obj in area.countryarea_set.all %}
<h4> {{ obj.name }} </h4>
{% endfor %}