Home>

If i have the following model, is there a way to set the username value to the nickname column by default when registering data from the form?

class User (models.Model):
    #Defining column names
    username = models.CharField (max_length = 10, unique = True)
    nickname = models.CharField (max_length = 10, unique = True)
class UserForm (forms.ModelForm):
    class Meta:
        model = User
        fields = ['username']

Rather than entering the nickname from the beginning, I asked if there was a way to set the default.

~ Additional ~
Sorry for not enough words.
For nickname, username is set as default, but it is assumed that the user can change it later.
In addition, it is assumed that it will not be displayed on the initial screen at the time of registration, and if there is no change, the default contents will continue to be used as they are.

  • Answer # 1

    Writing js code that uses the bi-directional binding as in Vue.js to enter the same thing in the nickname field with the username input, or allow empty input with nickname and submit in an empty state I think there is a way to set the same nickname as username on the Python side.

    [Vue.js bidirectional binding demo]
    https://jp.vuejs.org/v2/guide/index.html#%E3%83%A6%E3%83%BC%E3%82%B6%E3%83%BC%E5%85%A5%E5 % 8A% 9B% E3% 81% AE% E5% 88% B6% E5% BE% A1