[Fixed]-How to save username which submits the form?

1👍

User.get_username()

User represents the complete table, not the record. This is why you get an error.

So remove:
username = User.get_username()

A user might be able to change their username in your application, you can save a foreign key to the user instead. When you save the form do this:

if form.is_valid():
    print ('valid form')
    bet = form.save(commit=False) # the bet isn't saved just yet
    bet.user = request.user # you add the user here
    print bet.user.username
    bet.save() # finally save the bet in the database

In your Bet model, you need to add a field to record the user. For example:

class Bet(models.Model):
    ...
    user = models.ForeignKey(to=User, related_name="bets", blank=True, null=True)
    ...

Leave a comment