Wednesday, November 20, 2013

data not saving through a many to many relationship

In the view do_advice the specialism, delivery_method and face_to_face_locations selections are not being saved into the corresponding tables
of advicelevel_specialism, advicelevel_delivery_method and advicelevel_face_to_face_locations which are the fields of advicelevel_id  and x_id where x = specialism etc, normal many to many table right?
The actual AdviceLevel table is properly saved to with the advice_id, organisation_id, the selected levels etc everytning that is not a many to many effectively

Needless it say it works perfectly in Admin, just this user facing version doesnt work

Models:
class Organisation(models.Model):
    title = models.CharField(max_length=150)
    address1 = models.CharField(max_length=150, null=True, blank=True)
    address2 = models.CharField(max_length=150, null=True, blank=True)
    place = models.CharField(max_length=150, null=True, blank=True)
    postcode = models.CharField(max_length=15, null=True, blank=True)
    tel = models.CharField(max_length=30, null=True, blank=True)
    fax = models.CharField(max_length=30, null=True, blank=True)
    email = models.EmailField(null=True, blank=True)
    web = models.URLField()
    text = models.TextField()
    advice = models.ManyToManyField(Advice, through='AdviceLevel')   // note THROUGH
    crs = models.BooleanField()
    appointment_locations = models.TextField(null=True, blank=True)
    drop_in_locations = models.TextField(null=True, blank=True)
    def __unicode__(self):
         return u"%s" % self.title
    class Meta:
        ordering = ('title',)

class AdviceLevelKey(models.Model):
    key_level = models.CharField(max_length=150)
    key_level_text = models.TextField()
    key_level_cab = models.CharField(max_length=15)
    def __unicode__(self):
        return self.key_level

class AdviceLevel(models.Model):
    advice = models.ForeignKey(Advice)
    organisation = models.ForeignKey(Organisation)
    level_1 = models.BooleanField()
    level_2 = models.BooleanField()
    level_3 = models.BooleanField()
    level_4 = models.BooleanField()
    specialism = models.ManyToManyField(Specialism, null=True, blank=True)
    service_restriction = models.TextField(null=True, blank=True)
    delivery_method = models.ManyToManyField(CRSDeliveryMethod, null=True, blank=True)
    face_to_face_locations = models.ManyToManyField(Location, null=True, blank=True)
    service_supervisor = models.CharField(max_length=175, null=True, blank=True)
    def __unicode__(self):
        return u"%s (%s)" % (self.organisation, self.advice)

View // the offending one

class AdviceForm(ModelForm):
    class Meta:
        model = AdviceLevel
        exclude = ('organisation',)

def do_advice(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect('/user/login/')
    this_org = request.session['this_org']
    advkey = AdviceLevelKey.objects.all()
    org = Organisation.objects.get(pk=this_org)
    orgid = org.id
    organisation = ""
    help = OrganisationHelpTexts.objects.all()
    try:
        data_set = AdviceLevel.objects.all().filter(organisation=this_org)
    except AdviceLevel.DoesNotExist:
        data_set = None

    if request.POST:
        if 'editadvice' in request.POST:
            return HttpResponseRedirect('/directory/edit_advice/')
        if 'adviceform' in request.POST:
            adviceform = AdviceForm(request.POST)               
            if adviceform.is_valid():
                organisation = org
                adv = adviceform.cleaned_data['advice']
                service_restriction = adviceform.cleaned_data['service_restriction']
                service_supervisor = adviceform.cleaned_data['service_supervisor']
                specialism = adviceform.cleaned_data['specialism']
                delivery_method = adviceform.cleaned_data['delivery_method']
                face_to_face_locations = adviceform.cleaned_data['face_to_face_locations']
                level_1 = adviceform.cleaned_data['level_1']
                level_2 = adviceform.cleaned_data['level_2']
                level_3 = adviceform.cleaned_data['level_3']
                level_4 = adviceform.cleaned_data['level_4']
                if (level_3 == 0 and level_4 == 1) or (level_2 == 0 and (level_3 == 1 or level_4 == 1)) or (level_1 == 0 and (level_2 == 1 or level_3 == 1 or level_4 == 1)):
                    error = "That isn't a possible combination, the tiers are progressive"
                    adviceform = AdviceForm(request.POST)
                    return render_to_response('directory/advice.html', {'help': help, 'error': error, 'adviceform': adviceform, 'this_org': this_org, 'advkey': advkey, 'org': org, },context_instance=RequestContext(request))
                else:
                    af = adviceform.save(commit=False)
                    af.organisation = organisation
                    af.save()
                    adviceform = AdviceForm()
            else:
                adviceform = AdviceForm(request.POST)
    else:
        adviceform = AdviceForm()
    if data_set:
        adviceform = AdviceForm()
        return render_to_response('directory/advice.html', {'help': help, 'adviceform': adviceform, 'this_org': this_org, 'data_set': data_set, 'advkey': advkey, 'org':org, }, context_instance=RequestContext(request))
    else:
        return render_to_response('directory/advice.html', {'help': help, 'adviceform': adviceform, 'this_org': this_org, 'advkey': advkey, 'org': org,}, context_instance=RequestContext(request))







--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/638b47b1-78d8-4a68-9a4d-c3e7ce731008%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment