form not saving into user model Django - django

I'm currently processing a payment thing for an online subscription service and in order to get the users info to send this stuff, I have a payment form.
But, for some reason the payment form is not saving to the users account. Everything else actually processes and the only error I can trigger is a 'NOT NULL constraint failed: memberships_usermembership.user_id'
Here's what I have in my view -
#login_required()
def payments(request):
user_membership = get_user_membership(request)
selected_membership = get_selected_membership(request)
form = SubscriptionForm()
if request.method == "POST":
form_data = {
'full_name': request.POST['full_name'],
'email': request.POST['email'],
'phone_number': request.POST['phone_number'],
'country': request.POST['country'],
'postcode': request.POST['postcode'],
'town_or_city': request.POST['town_or_city'],
'street_address1': request.POST['street_address1'],
'street_address2': request.POST['street_address2'],
'county': request.POST['county'],
}
token = request.POST['stripeToken']
form = SubscriptionForm(form_data)
if form.is_valid():
customer = stripe.Customer.retrieve(
user_membership.stripe_customer_id)
customer.source = token
customer.save()
subscription = stripe.Subscription.create(
customer=user_membership.stripe_customer_id,
items=[
{"plan": selected_membership.stripe_plan_id},
]
)
user_membership = get_user_membership(request)
selected_membership = get_selected_membership(request)
user_membership.membership = selected_membership
user_membership.save()
form.save(commit=True)
subscription_id = subscription.id
sub, created = Subscription.objects.get_or_create(
user_membership=user_membership)
sub.stripe_subscription_id = subscription_id
sub.active = True
sub.save()
try:
del request.session['selected_membership_type']
except BaseException:
pass
return render(request, 'memberships/update-success.html')
else:
return redirect(reverse('membership_list'))
context = {
'selected_membership': selected_membership,
'form': form,
}
return render(request, 'memberships/payment.html', context)
When the form.save() is the line above the return(render) line, it will process everything as normal, and the form information just wont save into the DB.
It flashes the NOT NULL error when the form.save() line is where it in in the code above.
Any ideas how to get this working?
Thanks!
EDIT: Here's a link to the entire error in Django - http://dpaste.com/140VD8M
& a screenshot of it too!
Here's my models -
class Membership(models.Model):
membership_type = models.CharField(
choices=MEMBERSHIP_CHOICES,
default='Free',
max_length=30)
price = models.IntegerField(default=15)
description = models.TextField(default="DESCRIPTION")
image_url = models.URLField(max_length=1024, null=True, blank=True)
image = models.ImageField(null=True, blank=True)
stripe_plan_id = models.CharField(max_length=40)
def __str__(self):
return self.membership_type
class UserMembership(models.Model):
user = models.OneToOneField(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
full_name = models.CharField(max_length=50, null=True, blank=True)
email = models.EmailField(max_length=254, null=True, blank=True)
phone_number = models.CharField(max_length=20, null=True, blank=True)
country = CountryField(blank_label='Country', default="Ireland")
postcode = models.CharField(max_length=20, null=True, blank=True)
town_or_city = models.CharField(max_length=40, null=True, blank=True)
street_address1 = models.CharField(max_length=80, null=True, blank=True)
street_address2 = models.CharField(max_length=80, null=True, blank=True)
county = models.CharField(max_length=80, null=True, blank=True)
stripe_customer_id = models.CharField(max_length=40)
membership = models.ForeignKey(
Membership, on_delete=models.SET_NULL, null=True)
def __str__(self):
return self.user.username
def post_save_usermembership_create(
sender, instance, created, *args, **kwargs):
user_membership, created = UserMembership.objects.get_or_create(
user=instance)
if user_membership.stripe_customer_id is None or user_membership.stripe_customer_id == '':
new_customer_id = stripe.Customer.create(email=instance.email)
free_membership = Membership.objects.get(membership_type='Free')
user_membership.stripe_customer_id = new_customer_id['id']
user_membership.membership = free_membership
user_membership.save()
post_save.connect(post_save_usermembership_create,
sender=settings.AUTH_USER_MODEL)
class Subscription(models.Model):
user_membership = models.ForeignKey(
UserMembership, on_delete=models.CASCADE)
stripe_subscription_id = models.CharField(max_length=40)
active = models.BooleanField(default=False)
def __str__(self):
return self.user_membership.user.username
& here's my user membership def -
#login_required()
def get_user_membership(request):
user_membership_qs = UserMembership.objects.filter(user=request.user)
if user_membership_qs.exists():
return user_membership_qs.first()
return None

Instead of collecting all the form fields individually, you should be able to just pass the request.POST to the form like so:
form_data = request.POST
token = request.POST['stripeToken']
form = SubscriptionForm(form_data)
Other than that, without seeing the exact error message, there isn't much more to tell. If you update your question I will update my answer.
EDIT:
Without seeing your model and what get_user_membership() returns, it looks like you are missing a User object in a UserMembership class (but I can't tell without seeing more):
user_membership.user = request.user
Or something like that.

Related

foreign key in django

I have a model for musics and a model for comment of musics:
class music(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
STATUS_CHOICES = (('draft', 'Draft'), ('published', 'Published'),)
music = models.FileField()
music_image = models.ImageField(upload_to="images/")
singer_name = models.CharField(max_length=100)
music_name = models.CharField(max_length=100)
text_of_music = models.TextField()
create = models.DateField(auto_now_add=True, blank=True, null=True)
update = models.DateField(auto_now=True, blank=True, null=True)
publish = models.DateField(default=timezone.now, blank=True, null=True)
slug = models.CharField(max_length=250, unique_for_date='publish')
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')
objects = models.Manager()
published = PublishedManager()
class Meta:
ordering = ('-publish',)
def get_absolute_url(self):
return reverse('music:music_detail',
kwargs={"id":self.id})
class comment(models.Model):
# Foreignkey for each music
For = models.ForeignKey(music, on_delete=models.CASCADE, related_name='post')
body = models.CharField(max_length=500)
created_on = models.DateTimeField(auto_now_add=True)
active = models.BooleanField(default=True)
commented_by = models.ForeignKey(User, on_delete=models.CASCADE)
and this is my view:
def music_Detail(request, id=None):
user = request.user
template_name = 'music/music_detail.html'
Music = music.objects.all().filter(id=id)
new_comment = None
Comment = comment.objects.all().filter(active=True)
form = comment_form(data=request.POST)
if request.method == 'POST':
if form.is_valid():
new_comment = form.save(commit=False)
new_comment.For = Music
new_comment.save()
form = comment_form()
return render(request, template_name, {'Music': Music, 'Comment': Comment, 'form': form})
Well, I get this error when I comment:
Cannot assign "<QuerySet [<music: m, majid kharatha>]>": "comment.For" must be a "music" instance.
How can I solve this problem and how can I display the information of the user who left this comment?
As the error says, you'll have to assign a single Music, not a queryset.
Instead of filter()ing to get a new queryset containing a single music,
Music = music.objects.all().filter(id=id)
you want to get() a single one:
Music = music.objects.get(id=id)

Adding a comment section form to my Django app

I get 2 different errors the first one is
Django Model IntegrityError: NOT NULL constraint failed:
I checked the solution for this, people said I've to make my FK attributes null = true and blank = true
so this solved that.
The second error I got after that was in my views.py
'CommentForm' object has no attribute 'cleaned_data'
My Views.py file:
def project(request, pk):
form = CommentForm()
project = ProjectModel.objects.get(id=pk)
contextt ={
"project": project,
"form":form,
}
if request.method == "POST":
form.save()
return redirect(request, "/dashboard")
else:
return render(request, "projects.html", contextt)
and my Models.py:
class ProjectModel(models.Model):
caption = models.CharField(max_length=100)
video = models.FileField(upload_to="video/%y", validators=[file_size])
ProjectName = models.CharField(max_length=50, )
ProjectDescription = models.TextField(max_length=1000,)
Project_Background_picture = models.ImageField(
upload_to=settings.MEDIA_ROOT, default='/static/img/default.png')
Approved = models.BooleanField(default=False)
Finished = models.BooleanField(default=False)
Goal = models.DecimalField(
decimal_places=3, max_digits=6, blank=True, null=True)
Pledges = models.DecimalField(
decimal_places=3, max_digits=6, blank=True, null=True)
Number_of_investors = models.IntegerField(blank=True, null=True)
FirstReward = models.TextField(max_length=1000, default=' 10$ ')
SecondReward = models.TextField(max_length=1000, default=' 25$')
ThirdReward = models.TextField(max_length=1000, default='50$')
FourthReward = models.TextField(max_length=1000, default='100$ +s')
def __str__(self):
return self.caption
class comment(models.Model):
ProjectModel = models.ForeignKey(
ProjectModel, related_name='comments', on_delete=models.CASCADE, null=True, blank=True)
name = models.CharField(max_length=255)
CommentBody = models.TextField(default='comment here!', max_length=1000 )
date_added = models.DateTimeField(auto_now_add=True)
def _str_(self):
return '%s - %s' % (self.ProjectModel.caption, self.name)
try this:
def project(request, pk):
form = CommentForm()
project = ProjectModel.objects.get(id=pk)
contextt ={
"project": project,
"form ":form ,
}
if request.method == 'POST' :
form = CommentForm(data=request.POST)
if form.is_valid():
form= form.save(commit=False) #new line
form.ProjectModel = project #new line
form.save()
return redirect("/dashboard")
else:
return render(request, "projects.html", contextt)

Django save() method not saving to DB

What am I doing wrong?
I have two tables, User and Entrepreneur. Objects are not saving in the Entrepreneur table. I even used the shell!
I've deleted the DB and migration files and the error stays the same.
I know similar questions have been asked but I found none fitting my situation.
MY MODELS:
class EntrepreneurProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
mobile = models.CharField(max_length=100, blank=True, null=True)
location = CountryField(blank=True, null=True)
email_notif_on = models.BooleanField(default=False)
photo = models.ImageField(upload_to='photo/%Y/%m/%d', blank=True, null=True)
skills = TaggableManager(blank=True)
date_joined = models.DateField(auto_now_add=True)
accomplishnment = models.ForeignKey(Accomplishment, on_delete=models.CASCADE, null=True, blank=True)
portfolio = models.ManyToManyField(Portfolio, blank=True)
date_of_birth = models.DateField(blank=True, null=True)
url = models.SlugField(unique=True)
def __str__(self):
return f'{self.user}'
def save(self, *args, **kwargs):
if self.url:
self.url = slugify(f'{self.user.first_name} {self.user.last_name} {str(uuid.uuid4()[:7])}')
MY VIEWS:
def signup(request):
if request.method == 'POST':
user_form = UserCreateForm(request.POST)
if user_form.is_valid():
user = user_form.save(commit=False)
user.set_password(user.password)
user.save()
entrep = EntrepreneurProfile.objects.create(user=user)
entrep.save()
print(entrep.id)
return redirect('users:login')
else:
user_form = UserCreateForm()
data = {'user_form': user_form}
return render(request, 'users/signup.html', data)
It seems you forgot to call super().save(*args, **kwargs) in your save method as it is mentioned in
https://docs.djangoproject.com/en/2.2/topics/db/models/#overriding-predefined-model-methods

Cannot assign "<SimpleLazyObject: <User: admin>>": "Project.manager" must be a "Profil" instance

Hello I have this error when I start saving a form.
Cannot assign "<SimpleLazyObject: <User: admin>>": "Project.manager" must be a "Profil" instance.
I have two applications account and project
account.model.py
class Profil(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
photo_path = time.strftime('photo/%Y/%m/%d')
photo = models.ImageField(upload_to=PathAndRename(photo_path), blank=True, null=True)
antity = models.CharField(max_length=50, choices=ENTITY_TYPE, default=ENTITY_TYPE[0])
biography = models.TextField(blank=True, max_length=500)
location = models.CharField(max_length=30, blank=True, default='Congo-BZV')
facebook_url = models.URLField(default='', blank=True)
twitter_url = models.URLField(default='', blank=True)
inscrit_newsletter = models.BooleanField(default=True)
def __str__(self):
return "Profil de {0}".format(self.user.username)
project.model.py
class Project( models.Model):
manager = models.ForeignKey('accounts.Profil', related_name='project', on_delete=models.CASCADE)
title = models.CharField(max_length = 100)
slug = models.SlugField(unique=True)
image_path = time.strftime('images/%Y/%m')
main_image = models.ImageField(upload_to=PathAndRename(image_path), blank=True)
slogan = models.CharField(max_length=300, blank=True)
description = RichTextUploadingField(blank = True, null=True)
project.views.py
def project_new(request):
if request.method == 'POST':
form = ProjectForm(request.POST)
if form.is_valid():
project = form.save(commit = False)
project.manager = request.user
project.save()
else:
form = ProjectForm()
return render(request, 'project/project_new.html', {'form': form})
project.forms.py
class ProjectForm(forms.ModelForm):
title = forms.CharField(label='Titre')
description = forms.CharField(widget=CKEditorUploadingWidget())
category = forms.ModelChoiceField(queryset = Category.objects.all(), label='Catégorie')
#description = forms.CharField(widget=forms.Textarea(attrs={'cols': 80, 'rows': 20}))
class Meta:
model = Project
fields = ('title', 'nb_days', 'slogan', 'category', 'description')
I would like every user or manager to be linked to the project they post via a form.
How could I link the two models by saving a model?
Thank you
insted of
project.manager = request.user
you should use:
project.manager = request.user.profil

OneToOneField is nor related

this me again. I have some trouble with my code. I made a form from ModelForm which have a model with 8 attributes, but i want to user fill only one of them,and one from the system. The one that user fill is okay, but the one filled with system is not working.
models.py
class SeminarProposal(md.Model):
# diisi oleh mahasiswa
fileProposal = models.FileField()
# This is the one is filled with system
proposal = models.OneToOneField(Proposal,
on_delete=models.CASCADE,
related_name="propSid",
unique=True, blank=True, null=True)
masabimbingan = models.BooleanField(default=True)
# disi oleh admin
tanggal = models.DateField(default=timezone.now, blank=True,null=True)
tempat = models.CharField(max_length=30, blank=True, null=True)
# diisi oleh dosen pembimbing
dospemsetuju = models.BooleanField(default=False, blank=True)
# diisi oleh kaprodi
penguji1 = models.ForeignKey(Dosen,
on_delete=models.CASCADE,
related_name="penguji1",
blank=True, null=True)
penguji2 = models.ForeignKey(Dosen,
on_delete=models.CASCADE,
related_name="penguji2",
blank=True, null=True)
def __str__(self):
return "Sidang untuk " + self.proposal.judul
view.py
def daftarSeminar(request):
if request.method == 'POST':
form = FormSeminar(request.POST, request.FILES)
print(request.user)
if form.is_valid():
form.save(commit=False)
form.cleaned_data['proposal']
print(request.user)
prop = Proposal.objects.get(akun=request.user)
form.proposal = prop
print(form.proposal) #to confirm that this is not None
form.save()
return redirect('proposal:bimbingan')
else:
return render(request, 'sidprop.html' , {'oke': 'oke'})
return redirect('proposal:index')
form.py
class FormSeminar(forms.ModelForm):
class Meta:
model = SeminarProposal
fields = ['fileProposal','proposal']
Thanks in advance. Terimakasih.
Instead of changing the form data, get the object from form and change the object's data:
if form.is_valid():
obj = form.save(commit=False)
prop = Proposal.objects.get(akun=request.user)
obj.proposal = prop
obj.save()
return redirect('proposal:bimbingan')