hi,
I am new to this framework and was trying out some code..I created a
MySubject model with name and description fields.I am using name as a
unique field and description can be an empty string.I provided add and
edit views as well as a ModelForm.I wanted to deal with the following
usecases.
In add_subject
1.If the user enters a name that already is in the database,I would
like to warn the user that a subject of that name already exists.
2.if user mistakenly enters ' python' (with leading spaces )and a
subject 'python' is in db,and tries to modify the description,then I
would like to edit the original object 's description field.
I am having some doubts as to how to code the view for the second
usecase.
def add_or_edit_subject(request,instance=None):
...
if request.method=='POST':
form=MySubjectForm(request.POST,instance=instance)
if form.is_valid():
subname=form.cleaned_data['name']
if subject_is_new(subname.strip()):
form.save()
else:
#HOW TO DO THIS? I need to update the description
field
def subject_is_new(name):
sub=Subject.objects.filter(name__iexact =name.strip())
if len(sub)==0:
return True
else:
return False
class MySubject(models.Model):
name=models.CharField(unique=True,max_length=50)
description=models.TextField(blank=True)
class MySubjectForm(ModelForm):
class Meta:
model=MySubject
Can somebody please help with some pointers/suggestions.
thanks
jim
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.