0

enter image description hereI was following the official documentation of django and I need help with being able to add a option to the admin site where I can add a option to add a new field like along with customer name and password also an ID so how can i add the ID option on the page itself ?

Nikhar Bajaj
  • 11
  • 1
  • 4
  • In which model do you want the `ID` field – Bijoy Jul 25 '17 at 06:03
  • I want to add a option on the web page itself to add a new field whenever i am creating a new user – Nikhar Bajaj Jul 25 '17 at 06:21
  • If you want to add new field to default `User` model, then you can use `AbstractBaseUser` to extend the `User` model, or you can extend it with new model and using `OnetoOne` relation between them – Bijoy Jul 25 '17 at 06:27
  • this may be helpfull https://stackoverflow.com/questions/44109/extending-the-user-model-with-custom-fields-in-django – Bijoy Jul 25 '17 at 06:28
  • 1
    What page is 'the page'? The admin page? Your custom view page? Anything you did so far? @Bijoy is right, first add the field to your User model. Extend with a OneToOne field https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#extending-user OR substitute the existing User model https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#auth-custom-user – allcaps Jul 25 '17 at 06:32
  • see the picture attached suppose I want to add a new field while creating a new customer how can it be done ? – Nikhar Bajaj Jul 25 '17 at 06:55
  • Add the field to the model (like pointed out in the docs). After that unregister the existing User ModelAdmin and register a custom ModelAdmin containing your field. See the admin.py code here: https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#extending-the-existing-user-model – allcaps Jul 25 '17 at 07:12
  • i want a way to add the field to the model through the web page itself not using the models.py file – Nikhar Bajaj Jul 25 '17 at 08:36

1 Answers1

0

The easy way is create other class like CustomAdmin and use OnetoOne realation.

For example:

class CustomAdmin(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE,  blank=True, null=True, related_name='user_customUser')
username = models.CharField(max_length=100, null=True, blank=True, verbose_name='Username')
web_custom = models.CharField(max_length=100, null=True, blank=True, verbose_name='Web')
phone_custom = models.BigIntegerField(null=True, blank=True, verbose_name='Phone')
  • i think I wasnt able to explain my question properly you see the picture there when i am adding a customer there are 5 fields user,name ,server n ol what if I want to add a new field like apple_id there itself while creating a new user will this help? – Nikhar Bajaj Jul 25 '17 at 09:09