Hi. I’m building a simple Django app that allows to register, login and add (new, update, delete) records.
My problem is that in the video tutorial that I’m following everyone can see every records, and I need that user could see his/her records only. So I’m stuck. How do I add usernames to records, so I could filter them in view? Please help.
here is my model:
class Record(models.Model):
user = models.CharField(max_length=150)
creation_date = models.DateTimeField(auto_now_add=True)
sis = models.IntegerField()
dia = models.IntegerField()
pulse = models.IntegerField()
def __str__(self):
return str(self.creation_date) + " " + str(self.user) + " " + str(self.sis) + " " + str(self.dia) + " " + str(self.pulse)
here is forms.py class:
class CreateRecordForm(forms.ModelForm):
class Meta:
model = Record
fields = ['sis', 'dia', 'pulse']
here is view:
@login_required(login_url='my-login')
def create_record(request):
form = CreateRecordForm()
if request.method == "POST":
form = CreateRecordForm(request.POST)
if form.is_valid():
form.save()
return redirect("index")
context = {'form':form}
return render(request, 'core/create-record.html', context=context)
Ho do I add ‘current_user’ into ‘user’ field before form.save() ?
Thanks in advance.