class Advert(models.Model):
location_relation = models.ForeignKey("administrative_division.Towns", to_field='name_unique')
class Towns(models.Model):
name = models.CharField(max_length=128)
slug = models.SlugField()
voivodship = models.ForeignKey("Voivodships", to_field='voivodship_identifier')
county = models.ForeignKey("Counties")
name_unique=models.CharField(max_length=255, unique=True)
town_unique=models.BooleanField()
def __unicode__(self):
return "%s. %s"% (self.id, self.name)
class AdvertForm(ModelForm):
category = CustomTreeNodeChoiceField(queryset=Category.objects.filter(parent__isnull=False), empty_label="Wybierz kategorię", label="Kategoria")
class Meta:
model = Advert
exclude = ('ip', 'user')
widgets = {
'location_relation': TextInput
}
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None)
super(AdvertForm, self).__init__(*args, **kwargs)
def clean_location_relation(self):
from string import capitalize
from django import forms
from administrative_division.models import Towns
cleaned_data = self.cleaned_data['location_relation']
try:
cleaned_data = Towns.objects.get(name_unique=capitalize(cleaned_data.name_unique))
except:
raise forms.ValidationError("Such city does not exist")
return cleaned_data
我使用文本输入表单字段而不是选择字段。所以我需要手动提供唯一的名称。
我需要将提供的字符串大写到名为 location_relation 的字段中。我尝试了不同的选项,例如在模型中使用清洁方法,现在我尝试以模型形式进行清洁。
我现在真的不知道该怎么做。
请给我一些提示或解决此问题的方法:)