Exception Value: | UNIQUE constraint failed: accounts_profile.user_id |
---|
/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/backends/utils.py
in execute
Variable | Value |
---|---|
params | |
self | |
sql |
Variable | Value |
---|---|
__class__ | |
context | |
form | |
profile_formset | |
self |
class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE)
location = models.CharField(max_length=30, blank=True) birthdate = models.DateField(null=True, blank=True)
profile_picture = models.ImageField(upload_to='profile_pictures', blank=True)
def __str__(self): return self.user.username
@property def age(self): TODAY = datetime.date.today() if self.birthdate: return u"%s" % relativedelta.relativedelta(TODAY, self.birthdate).years else: return None
@receiver(post_save, sender=User)def create_or_update_user_profile(sender, instance, created, **kwargs): if created: Profile.objects.create(user=instance) instance.profile.save()
class FormSignUp(UserCreationForm): email = forms.CharField(max_length=254, required=True, widget=forms.EmailInput())
class Meta: model = User fields = ['username', 'email', 'password1', 'password2']
class FormProfile(forms.ModelForm): class Meta: model = Profile fields = ['birthdate', 'gender', 'site_facebook', 'site_twitter', 'profile_picture']
FormSetProfile = inlineformset_factory(User, Profile, form=FormProfile)
class CreateUser(CreateView): form_class = FormSignUp template_name = 'signup.html' success_url = reverse_lazy('home')
def get_context_data(self, **kwargs): data = super(CreateUser, self).get_context_data(**kwargs) data['profile'] = FormSetProfile(self.request.POST) else: data['profile'] = FormSetProfile() return data
def form_valid(self, form): context = self.get_context_data() profile_formset = context['profile']
with transaction.atomic(): form.instance.created_by = self.request.user form.instance.updated_by = self.request.user self.object = form.save() if profile_formset.is_valid(): profile_formset.instance = self.object profile_formset.save() # Linha onde está dando o erro
return super(CreateUser, self).form_valid(form)
--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ec6cbe06-6252-408f-a36c-7b863b33ad6c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.