Nerii Doron
Nerii Doron

Reputation: 43

django Models getting user info

trying to create a model that has an artist and song and lets you know what user name typed it in. so far I have in my models.py

from django.contrib.auth.models import User


class Song(models.Model):

uesrname = models.ForeignKey(User,on_delete=models.CASCADE)
artist = models.CharField(max_length=30)
song=models.CharField(max_length=30)

I added a form that works with user input data but the form lets me select one of all exciting Users and input artist, song

forms.py
class NewSong(forms.ModelForm):
    class Meta:
        model=Song
        exclude = ['username']

how can I change it so I will have only my own loged in user in the form?

Upvotes: 0

Views: 406

Answers (1)

Deniz Kaplan
Deniz Kaplan

Reputation: 1609

You can exclude username field as exclude = ['username'] on your forms.py file and set username to request.user on form's save method. For more information: selecting fields

Upvotes: 1

Related Questions