many to many intermediary - template - model form
Hi,
Below are my models, forms, views, template snippets. I am unable to
code the django techniques well as I would like to use form to enable
me do validation, POST extraction etc. It seems when using many to
many key we cannot make good use of stuff.
can one point me to good example which can show how I can code the
logic well. In template I want
firstname, lastname, date, phone and email (phone, email are action
types I manually configured in entity ActionType). So once user enter
them I want model form to also help in handling it. I am unable to
get it going as probably I do not know correct usage.
Please guide on best way to code it.
models.py (I am storing birthdays and with each I store intimation
actions like phone, email as way to notify greetings)
========
from django.db import models
from datetime import date, datetime
from django.contrib.auth.models import User
class ActionType(models.Model):
name = models.CharField(max_length=64)
class Event(models.Model):
date = models.DateField()
user = models.ForeignKey(User)
actionTypes = models.ManyToManyField(ActionType, through='Action')
def occursToday(self):
pass
class Birthday(Event):
firstName = models.CharField(max_length=64)
lastName = models.CharField(max_length=64)
def occursToday(self):
todayDate = date.today()
bd = datetime.strptime(self.date, "%Y-%m-%d")
return bd.day == todayDate.day and bd.month == todayDate.month
def __unicode__(self):
return "Birthday: %s %s %s" % (self.firstName, self.lastName,
self.date)
class Action(models.Model):
actionType = models.ForeignKey(ActionType)
event = models.ForeignKey(Event)
value = models.CharField(max_length=64)
forms.py
======
from event.models import Birthday
from django.forms import ModelForm
class CreateBirthdayForm(ModelForm):
class Meta:
model = Birthday
views.py
======
//excluding imports
def create_birthday(request):
context = RequestContext(request)
if request.method == 'POST':
#form = CreateBirthdayForm(request.POST)
#if form.is_valid():
# print form.cleaned_data
# print form.cleanded_data['first_name'],
form.cleaned_data['last_name'], form.cleaned_data['date']
firstName = request.POST['first_name']
lastName = request.POST['last_name']
date = request.POST['date']
phone = request.POST['phone']
email = request.POST['email']
if !firstName or !lastName or !date or !phone or !email:
return render_to_response('birthday/error.html') #replace with
list_users
else:
print form.cleaned_data
print "form invalid"
return render_to_response('birthday/success.html') #replace with
list_users
else:
form = CreateBirthdayForm()
actionTypes = ActionType.objects.all()
return render_to_response('birthday/create_birthday.html',
{'form': form, 'actionTypes' : actionTypes}, context_instance=context)
template:
{% extends "birthday/base.html" %}
{% block content %}
<form action="" method="post">
{% csrf_token %}
<label for="id_first_name">First Name:</label>
{{ form.firstName }}
<br/>
<label for="id_last_name">Last Name:</label>
{{ form.lastName }}
<br/>
<label for="id_date">Birthdate:</label>
{{ form.date }}
<br/>
{% for actionType in actionTypes %}
<label for"id_{{ actionType.name|
lower }}">{{ actionType.name }}:<input type="text"
name="{{ actionType.name|lower }}" maxlength="64"/>
<br/>
{% endfor %}
<p><input type="submit" value="Create New"/></p>
</form>
<br/>
or
<hr/>
<form action="/birthday/create/bulk_excel" method="POST"
enctype="multipart/form-data">
{% csrf_token %}
<input type="file" name="file"/>
<INPUT type="submit" value="Bulk Upload"> <INPUT type="reset">
</form>
<br/>
<a href="{{ request.META.HTTP_REFERER }}">Back</a>
{% endblock %}
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home