Найти - Пользователи
Полная версия: что делать если одинаковые имена полей в modelform?
Начало » Django » что делать если одинаковые имена полей в modelform?
1
LestatCheb
имеются следующие модели
class template_people(models.Model):
name = models.CharField(max_length=255, verbose_name=u'Имя')
lastname = models.CharField(max_length=255, verbose_name=u'Фамилия')
fathername = models.CharField(max_length=255, verbose_name=u'Отчество')

class Meta:
abstract = True

class People1(template_people):

def __unicode__(self):
return u'%s %s %s [id: %s]' % (self.lastname, self.name, self.fathername, self.id)

class Meta:
verbose_name_plural = "People1"

class People2(template_people):

def __unicode__(self):
return u'%s %s %s [id: %s]' % (self.lastname, self.name, self.fathername, self.id)

class Meta:
verbose_name_plural = "People2"
и следующие формы:
class People1Form(forms.ModelForm):
class Meta:
model = People1

class People2Form(forms.ModelForm):
class Meta:
model = People2
проблема в том, что в HTML имена у них получаются одинаковыми:
    <form action="" method="post">

<table>

<TR><TD><br> <b>people1</b> </TD></TR>

<tr><th><label for="id_name">Имя:</label></th><td><input id="id_name" type="text" name="name" maxlength="255" /></td></tr>
<tr><th><label for="id_lastname">Фамилия:</label></th><td><input id="id_lastname" type="text" name="lastname" maxlength="255" /></td></tr>
<tr><th><label for="id_fathername">Отчество:</label></th><td><input id="id_fathername" type="text" name="fathername" maxlength="255" /></td></tr>

<TR><TD><br> <b>people2</b> </TD></TR>

<tr><th><label for="id_name">Имя:</label></th><td><input id="id_name" type="text" name="name" maxlength="255" /></td></tr>

<tr><th><label for="id_lastname">Фамилия:</label></th><td><input id="id_lastname" type="text" name="lastname" maxlength="255" /></td></tr>
<tr><th><label for="id_fathername">Отчество:</label></th><td><input id="id_fathername" type="text" name="fathername" maxlength="255" /></td></tr>


</table>

<input type="submit" value="Сохранить">

</form>
т.е. то что я забью в первую форму, просто проигнорируется, и в первую и во вторую таблицу в базе забьется инфа из последней формы.
как это решить?


заранее спасибо!
LestatCheb
пробовал так, не помогает :(

class People1Form(forms.ModelForm):
class Meta:
model = People1
def __init__(self, *args, **kwargs):
super(People1Form, self).__init__(*args, ** kwargs)
self.fields['name'].widget=(forms.TextInput(attrs={'name':'People1Form_name'}))
LestatCheb
хы, можно так изменить :)

class People1Form(forms.ModelForm):
class Meta:
model = People1
def __init__(self, *args, **kwargs):
super(People1Form, self).__init__(*args, ** kwargs)
name_var = self.fields.get('name')
self.fields['people1form_name'] = name_var
self.fields.pop('name')
lastname_var = self.fields.get('lastname')
self.fields['people1form_lastname'] = lastname_var
self.fields.pop('lastname')
fathername_var = self.fields.get('fathername')
self.fields['people1form_fathername'] = fathername_var
self.fields.pop('fathername')

class People2Form(forms.ModelForm):
class Meta:
model = People2
def __init__(self, *args, **kwargs):
super(People2Form, self).__init__(*args, ** kwargs)
name_var = self.fields.get('name')
self.fields['people2form_name'] = name_var
self.fields.pop('name')
lastname_var = self.fields.get('lastname')
self.fields['people2form_lastname'] = lastname_var
self.fields.pop('lastname')
fathername_var = self.fields.get('fathername')
self.fields['people2form_fathername'] = fathername_var
self.fields.pop('fathername')
тогда действительно в формах имена полей будут разные, но джанга уже данных при сохранении не находит :)

или не париться с наследованием и сделать тупо
class People1(models.Model):
name1 = models.CharField(max_length=255, verbose_name=u'Имя')
lastname1 = models.CharField(max_length=255, verbose_name=u'Фамилия')
fathername1 = models.CharField(max_length=255, verbose_name=u'Отчество')
class People2(models.Model):
name2 = models.CharField(max_length=255, verbose_name=u'Имя')
lastname2 = models.CharField(max_length=255, verbose_name=u'Фамилия')
fathername2 = models.CharField(max_length=255, verbose_name=u'Отчество')
но по идее так не делают, да ведь? :)
PooH
Form.prefix
You can put several Django forms inside one <form> tag. To give each Form its own namespace, use the prefix keyword argument:
>> mother = PersonForm(prefix="mother")
>>> father = PersonForm(prefix="father")
>>> print mother.as_ul()
<li><label for="id_mother-first_name">First name:</label> <input type="text" name="mother-first_name" id="id_mother-first_name" /></li>
<li><label for="id_mother-last_name">Last name:</label> <input type="text" name="mother-last_name" id="id_mother-last_name" /></li>
>>> print father.as_ul()
<li><label for="id_father-first_name">First name:</label> <input type="text" name="father-first_name" id="id_father-first_name" /></li>
<li><label for="id_father-last_name">Last name:</label> <input type="text" name="father-last_name" id="id_father-last_name" /></li>
LestatCheb
PooH
Form.prefix
You can put several Django forms inside one <form> tag. To give each Form its own namespace, use the prefix keyword argument:
все гениальное просто :)

спасибо огромное! все заработало :)
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB