i am having one tag class and one user in which user contained a tag
element which has manytomany relation with tag class
can you guyz tell me how to save this tag using form....
i attached the form.py model.py and view.py
my problem is the tag element i can't access through the form and don't
understand how to save it... i tried lot's ya.... can you guyz help.
thanx in advance.
--
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+unsubscr...@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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
from django import forms
from application.homepage.models import Tag
class RegistrationForm(forms.Form):
name = forms.CharField()
dob = forms.DateTimeField()
email = forms.EmailField()
alternate_email=forms.EmailField()
permission=forms.DecimalField()
password = forms.CharField()
phone=forms.DecimalField()
#pic = forms.ImageField()
about = forms.CharField(widget=forms.Textarea)
profile_view = forms.DecimalField()
location = forms.CharField(widget=forms.Textarea)
website = forms.CharField()
reputation=forms.DecimalField()#tags= forms.CharField(widget=forms.CheckboxSelectMultiple(choices=choice))
tags= forms.ModelMultipleChoiceField(queryset=Tag.objects.all(),widget=forms.SelectMultiple())
num_of_que_asked = forms.DecimalField()
num_of_que_answered = forms.DecimalField()
doj = forms.DateField(widget=forms.DateInput)
from thumbs import ImageWithThumbsField
from django.db import IntegrityError
from django.db import models
class Tag(models.Model):
name = models.CharField(max_length=64, primary_key=True)
about = models.TextField()
created_date = models.DateTimeField(auto_now_add=True)
editor = models.CharField(max_length=64)
active = models.BooleanField()
verified = models.BooleanField()
rating = models.DecimalField(max_digits=8,decimal_places=0)
date_modified = models.DateTimeField(auto_now_add=True)
def __str__(self):
#return (self.name,self.about,self.created_date,self.editor,self.active,self.verified,self.rating,self.viewed,self.date_created)
return (self.name)
class Admin:
pass
class User(models.Model):
name = models.CharField(max_length=64)
dob = models.DateTimeField(auto_now_add=True)
email = models.EmailField(verbose_name='your e-mail')
alternate_email=models.EmailField()
permission=models.DecimalField(max_digits=3,decimal_places=0,blank=True, null=True)
password = models.CharField(max_length=32)
phone=models.DecimalField(max_digits=11,decimal_places=0)
pic = ImageWithThumbsField(upload_to='images',blank=True, null=True)
about = models.TextField()
profile_view = models.DecimalField(max_digits=8,decimal_places=0)
location = models.TextField()
website = models.URLField(max_length=64)
reputation=models.DecimalField(max_digits=3,decimal_places=0,blank=True, null=True)
tags= models.ForeignKey(Tag, blank=True, null=True)
num_of_que_asked = models.DecimalField(max_digits=8,decimal_places=0)
num_of_que_answered = models.DecimalField(max_digits=8,decimal_places=0)
doj = models.DateTimeField(auto_now_add=True)
def __str__(self):
#return (self.name,self.dob,self.email,self.about,self.profile_view,self.location,self.website,self.tags,self.num_of_que_asked,self.num_of_que_answered,self.doj)
return (self.name)
class Admin:
pass
class Tips(models.Model):
heading = models.CharField(max_length=64)
description = models.TextField()
verified = models.BooleanField(blank=True)
creation_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now_add=True)
tags = models.ManyToManyField(Tag)
active = models.BooleanField(blank=True)
created_by = models.CharField(max_length=64)
rating = models.DecimalField(max_digits=8,decimal_places=0)
viewed = models.DecimalField(max_digits=8,decimal_places=0,blank=True, null=True)
def __str__(self):
#return (self.heading,self.about,self.verified,self.creation_date,self.modified_date,self.tags,self.created_by,self.rating,self.viewed)
return (self.heading)
class Admin:
pass
# Create your views here.
from django.shortcuts import render_to_response, get_object_or_404
from application.homepage.models import Tips,User,Tag
from django.http import HttpResponse
from django.template import RequestContext
from application.homepage.forms import RegistrationForm
def index(request):
userlist=Tips.objects.all()
mrel=userlist[0].tags.all()
return render_to_response('homepage/index.html',{'userlist':userlist, 'mrel':mrel})
def register(request):
if request.method == 'POST':
reg_form=RegistrationForm(request.POST, request.FILES)
taglist=[]
print "working"
if reg_form.is_valid():
success=True
try:
reg=User.objects.get_or_create(pk=1)
reg.name = reg_form.cleaned_data['name']
reg.dob = reg_form.cleaned_data['dob']
reg.email = reg_form.cleaned_data['email']
reg.alternate_email= reg_form.cleaned_data['alternate_email']
reg.permission=9
reg.password = reg_form.cleaned_data['password']
reg.phone= reg_form.cleaned_data['phone']
reg.about = reg_form.cleaned_data['about'] #name,dob,email,alternate_email, permission, password,phone,about=about
reg.profile_view = reg_form.cleaned_data['profile_view'] #profile_view=profile_view, location=location, website=website
reg.location = reg_form.cleaned_data['location']
reg.website = reg_form.cleaned_data['website']
reg.reputation=9
tags= reg_form.cleaned_data['tags']
taglist=[Tag.objects.get(name=tag.name) for tag in tags]
print taglist
reg.num_of_que_asked = reg_form.cleaned_data['num_of_que_asked']
reg.num_of_que_answered = reg_form.cleaned_data['num_of_que_answered']
reg.doj = reg_form.cleaned_data['doj']
#if 'pic' in request.FILES:
# pics= request.FILES['pic'].name
# user=User()
#user.pic.save(pics,request.FILES['pic'])
# pic=get_object_or_404(User,id=user.id)
#else:
# pic="null"
#use=User.objects.get(pk=2)
#use.save()
for tag in taglist:
reg.tags.add(tag)
except Exception:
print "Exception"
#reg=User(name=name,dob=dob,email=email,alternate_email=alternate_email, permission=permission, password=password,phone=phone,about=about,profile_view=profile_view, location=location, website=website,reputation=reputation)
reg.save()
reg.save()
else:
reg_form=RegistrationForm()
ctx={'reg_form':reg_form}
return render_to_response('register.html',ctx,context_instance=RequestContext(request))