0

I am new in Django and I really need help,

I do not know how to save my form data to database. I have problem to views.py

I will user's id who filled the form added into the foreign key field.

If there is any link or example that help me I appreciate you.

# views.py

@login_required(login_url="home")

def melk_new(request):

form = MelkForm()

???    

return render(request, 'melk_new.html',{'form': form})



# models.py

class Melk(models.Model):

category = models.CharField(max_length=50)

city = models.CharField(max_length=100)

person = models.ForeignKey('CustomUser', on\_delete=models.CASCADE)

def \_\_str\_\_(self):
return self.category    

class CustomUser(AbstractUser):

def __str__(self):

return self.email   

---------------------------------------------------------------------------

# forms.py

class MelkForm(forms.ModelForm):

class Meta:

model = Melk

fields = ('category', 'city')    

class CustomUserCreationForm(UserCreationForm):

class Meta(UserCreationForm):

model = CustomUser

fields = ('username', 'email')
Alireza20
  • 1
  • 1
  • 2
  • What you would like to do is remove the form element (by making custom form), and always put the 'request-user' in the value just before saving the model. The way to adjust the form depends if you are aiming on a frontend or backend (django admin) page... – Tom V. Mar 30 '19 at 18:04
  • @ Tom V Thank you for the reply. is there any link or example, – Alireza20 Mar 30 '19 at 20:44
  • Here is an example how to 'hacky' remove a field from an form: https://stackoverflow.com/a/15557335/5229859 – Tom V. Mar 31 '19 at 12:32

2 Answers2

0

To get the currently logged in user you'll find it within request.user object. but before you assume that there is a currently logged in user, you need to validate that so you have 2 widely known options:

  1. request.user.is_authenticated()
  2. @login_required() decorator used if you from django.contrib.auth.decorators import login_required

    if request.user.is_authenticated():
        Car.objects.create(model_id=some_id, person=request.user)
    

Note:

The @login_required() is added above the function

Ramy M. Mousa
  • 5,727
  • 3
  • 34
  • 45
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – double-beep Mar 31 '19 at 11:21
  • @double-beep I edited my answer, apologies was in hurry – Ramy M. Mousa Mar 31 '19 at 12:49
  • Thank you for the reply. I do not know what can i write instead of some_id. Django takes error on some_id. – Alireza20 Mar 31 '19 at 13:16
  • @Alireza20 some_id is just a demonstration to replace with your desired attributes to fill the data model, in your case it will be category_id instead of model_id passing the captured category id selected by your user. – Ramy M. Mousa Apr 02 '19 at 23:33
0
@login_required(login_url="home")
 def melk_new(request):
    form = MelkForm(request.POST)
    if form.is_valid():
        instance = form.save(commit=False)
        instance.user_id = request.user.id
        instance.save()
    return render(request,'melk_new.html', { 'form': form})
Alireza20
  • 1
  • 1
  • 2