0

I've a problem. My form saves in views, but then in database it is all empty.

In views, I'm getting parsing JSON, and storing it in eventsParsed model, so then I can update actual from.

I know my code is a bit messy. Can you please advice me how to properly clean it too?

views.py

    args['form'] = TourCreationForm()
if request.method == 'POST':
    form = TourCreationForm(request.POST, request.FILES)
    if 'dates' in request.POST:
        eventsParsed = EventsModel()
        eventsParsed.save()
        jsonDates = request.POST['dates']
        dates = json.loads(jsonDates)
        for date in dates:
            event = CalendarEvent(title=date['title'], start=date['start'], end=date['end'], all_day=date['allDay'])
            event.save()
            eventsParsed.datesForPost.add(event)
        updated_data = request.POST.copy()
        updated_data.__setitem__('dates', eventsParsed.datesForPost)
        form = TourCreationForm(data=updated_data )
    if form.is_valid():
        form.save()

models.py

from django.db import models
from taggit.managers import TaggableManager
from django.core.validators import MinValueValidator
from decimal import *
import os.path
# Create your models here.

def image_upload_path(instance, filename):
    return os.path.join("covers/", filename)

class CalendarEvent(models.Model):
    title = models.CharField(blank=True, max_length=200)
    start = models.DateTimeField()
    end = models.DateTimeField()
    all_day = models.BooleanField(default=False)

    def __unicode__(self):
        return self.title

class EventsModel(models.Model):
    datesForPost = models.ManyToManyField(CalendarEvent)

class Tours(models.Model):
    tour = models.Manager()
    #Visible/Non-visible tour
    publish = models.BooleanField(default=False)
    username = models.CharField(max_length=200,blank=True)
    ####################



    #Head properties
    title = models.CharField(max_length=200)
    slug = models.SlugField(max_length=200,unique=True,blank=True,editable=False)
    location = models.CharField(max_length=200)
    ################

    #Text Areas
    description = models.TextField()
    expect = models.TextField()
    rules = models.TextField()
    ############

    #(Un)available dates
    dates = models.ManyToManyField(CalendarEvent)

    #Price
    price = models.DecimalField(max_digits=6, decimal_places=2,default=Decimal('0.00'),validators=[MinValueValidator(Decimal('0.01'))])
    ############

    #Checks for type
    adventure_type_check = models.BooleanField(default=False)
    night_type_check = models.BooleanField(default=False)
    art_type_check = models.BooleanField(default=False)
    family_type_check = models.BooleanField(default=False)
    food_type_check = models.BooleanField(default=False)
    photography_type_check = models.BooleanField(default=False)
    shopping_type_check = models.BooleanField(default=False)
    party_type_check = models.BooleanField(default=False)
    relax_type_check = models.BooleanField(default=False)
    active_type_check = models.BooleanField(default=False)
    ######################

    #Checks for conditions
    group_check = models.BooleanField(default=False)
    risky_check = models.BooleanField(default=False)
    outside_check = models.BooleanField(default=False)
    photo_check = models.BooleanField(default=False)
    video_check = models.BooleanField(default=False)
    ######################

    #Images wide/normal
    wide_img_1 = models.ImageField(upload_to = image_upload_path, blank=True)
    wide_img_2 = models.ImageField(upload_to = image_upload_path, blank=True)
    wide_img_3 = models.ImageField(upload_to = image_upload_path, blank=True)
    normal_img_1 = models.ImageField(upload_to = image_upload_path, blank=True)
    normal_img_2 = models.ImageField(upload_to = image_upload_path, blank=True)
    normal_img_3 = models.ImageField(upload_to = image_upload_path, blank=True)
    normal_img_4 = models.ImageField(upload_to = image_upload_path, blank=True)
    ######################

    #Rest Properties of the tour
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)
    tags = TaggableManager()
    ############################

    def __str__(sefl):
        return sefl.title

    class Meta:
        verbose_name = "Tour Entry"
        verbose_name_plural = "Tour Entries"
        ordering = ["-created"]

forms.py

from django import forms
from django.core.exceptions import ValidationError
from django.core.validators import MinValueValidator
from taggit.managers import TaggableManager
from tours.models import Tours
from django.utils.text import slugify
from decimal import *
import os.path

class TourCreationForm(forms.ModelForm):

    class Meta:
        model = Tours
        fields = '__all__'
Elyor
  • 5,396
  • 8
  • 48
  • 76
yeralin
  • 1,357
  • 13
  • 24

1 Answers1

1

You seem to be skipping the case where form.is_valid() returns false--are you sure the form you're posting is valid? Perhaps add an else and check for errors or re-display the form template (which automatically shows errors). The typical format to follow is here: https://docs.djangoproject.com/en/1.8/topics/forms/#the-view

Also, I'm not sure you want to use tour = models.Manager(). Perhaps a ForeignKey would be more appropriate. Not sure if this is the problem with the form, since you seem to have cropped some of the view code.

David G
  • 318
  • 2
  • 8
  • Nope, I'm debugging it. It shows that form is valid, and all fields are correct. But then in DB, record exists, but all fields are empty, except parsed JSON: field "dates" – yeralin Jul 15 '15 at 12:11
  • Also, do you know how to simplify my models.py For example there is a bunch of booleanfields. I can separate them into different model, and then in main model create Foreign Key to that model. But after that, I'll have to parse my checkboxes manually to that Foreign Key, right? Is there a simple way? – yeralin Jul 15 '15 at 12:24
  • Also, I referred to this source: About parsing JSON, before validation http://stackoverflow.com/questions/17304148/set-form-field-value-before-is-valid – yeralin Jul 15 '15 at 12:27