-2
我正在創建一個表單,以便用戶可以在配置文件模型中將他們的數據與其數據一起註冊,問題出在ManyToMany類別字段,並且在保存時給我提供了錯誤。從django中的模板創建新用戶時出錯
這是我的表格。
class UserForm(UserCreationForm):
negocio = forms.CharField(max_length=30)
encargado = forms.CharField(max_length=30)
imagen = forms.ImageField()
direccion = forms.CharField(max_length=30)
telefono = forms.DecimalField(max_digits=15, decimal_places=0)
email = forms.EmailField()
categoria = forms.ModelMultipleChoiceField(queryset=Categoria.objects.all())
lat = forms.CharField(max_length=30)
lng = forms.CharField(max_length=30)
這些是我的模特。
class Perfil(models.Model):#Este es el perfil del usuario(solo existe uno por cada usuario)
user = models.OneToOneField(User, related_name='profile')
nombre_negocio = models.CharField(max_length=50, unique=True)
encargado = models.CharField(max_length=50,null=True,blank=True)
imagen = models.ImageField(upload_to = 'perfiles',null=True,blank=True)
direccion = models.CharField(max_length=100)
telefono = models.DecimalField(max_digits=15, decimal_places=0,null=True,blank=True)
email = models.EmailField()
category = models.ManyToManyField(Categoria)
inicio = models.DateTimeField(null=True,blank=True)
final = models.DateTimeField(null=True,blank=True)
lat = models.CharField(max_length=50,null=True,blank=True)
lng = models.CharField(max_length=50,null=True,blank=True)
def save(self, *args, **kwargs):
if not self.id:
self.slug = slugify(self.nombre_negocio)
super(Perfil, self).save(*args, **kwargs)
def __unicode__(self):
return self.nombre_negocio
class Categoria(models.Model):
name = models.CharField(max_length=50)
slug = models.SlugField(editable=False)
imagen = fields.ImageField(upload_to = 'categorias',null=True,blank=True,dependencies=[
FileDependency(processor=ImageProcessor(
format='JPEG', scale={'max_width': 200, 'max_height': 150}))
])
def save(self, *args, **kwargs):
if not self.id:
self.slug = slugify(self.name)
super(Categoria, self).save(*args, **kwargs)
def __unicode__(self):
return self.name
這是我的看法。
class RegistrarUsuario(FormView):
form_class = UserForm
template_name = 'home/registrar.html'
success_url = '/registrar'
def form_valid(self,form):
user = form.save()
email = form.cleaned_data['email']
direccion = form.cleaned_data['direccion']
nombre_negocio = form.cleaned_data['negocio']
category = form.cleaned_data['categoria']
encargado = form.cleaned_data['encargado']
telefono = form.cleaned_data['telefono']
imagen = form.cleaned_data['imagen']
lat = form.cleaned_data['lat']
lng = form.cleaned_data['lng']
user_profile = Perfil(user=user,nombre_negocio=nombre_negocio,encargado=encargado,imagen=imagen,direccion=direccion,telefono=telefono,email=email,category=category,lat=lat,lng=lng)
user.save()
user_profile.save()
return super(RegistrarUsuario, self).form_valid(form)
而我不知道如何做好將數據保存在分類領域。
如果你得到一個錯誤,你應該告訴我們它是什麼。 –
不好意思的朋友,問題是類別類別。 TypeError at/registrar/ 'category'是此函數的無效關鍵字參數 –