1062, "Duplicate entry 'admin1' for key 'username'"

models.py

class CustomUser(AbstractUser):
    user_type_data=((1,"HOD"),(2,"Staff"),(3,"Student"))
    user_type=models.CharField(default=1,choices=user_type_data,max_length=10)

class palabout(models.Model):
    user = models.ForeignKey(CustomUser, blank=True, null=True, on_delete=models.SET_NULL)
    profileImage = models.FileField()
    username = models.CharField(max_length=30)
    email = models.EmailField(max_length=100)
    password = models.CharField(max_length=100)
    fname = models.CharField(max_length=30)
    lname = models.CharField(max_length=30)
    gender = models.CharField(
        max_length=1, choices=(('m', ('Male')), ('f', ('Female'))),
        blank=True, null=True)
    dob = models.DateField(max_length=8)

forms.py

class palForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput)
    class Meta:
        model=palabout
        fields =['username','password','email','fname','lname','dob','gender','profileImage']

views.py

from .forms import palForm

def add_form(request):
        form = palForm(request.POST, request.FILES)
        username=request.POST.get("username")
        email=request.POST.get("email")
        password=request.POST.get("password")
        if request.method == "POST":
           form = palForm(request.POST  , request.FILES)
      user=CustomUser.objects.create_user(username=username,password=password,email=email,user_type=1)
        if form.is_valid() :
          try:
            form.save()
            messages.success(request,"Successfully Added")
            return render(request,"home.html")
          except:
            messages.error(request,"Failed to Add")
            return render(request,"home/pal-form.html")
        else:
            form=palForm()
            return render (request,"home/pal-form.html",context={"form":form})

Error:

Traceback (most recent call last): File "C:\Users\Anaconda3\lib\site-packages\django\core\handlers\exception.py", line 55, in inner response = get_response(request) File "C:\Users\Anaconda3\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "C:\Users\Desktop\myschool\views.py", line 19, in polabout CustomUser.objects.create_user(username=username,password=password,email=email,user_type=3) File "C:\Users\Anaconda3\lib\site-packages\django\contrib\auth\models.py", line 161, in create_user return self._create_user(username, email, password, **extra_fields) File "C:\Users\Anaconda3\lib\site-packages\django\contrib\auth\models.py", line 155, in _create_user user.save(using=self._db) File "C:\Users\Anaconda3\lib\site-packages\django\contrib\auth\base_user.py", line 68, in save super().save(*args, **kwargs) File "C:\Users\Anaconda3\lib\site-packages\django\db\models\base.py", line 812, in save self.save_base( File "C:\Users\Anaconda3\lib\site-packages\django\db\models\base.py", line 863, in save_base updated = self._save_table( File "C:\Users\Anaconda3\lib\site-packages\django\db\models\base.py", line 1006, in _save_table results = self._do_insert( File "C:\Users\Anaconda3\lib\site-packages\django\db\models\base.py", line 1047, in _do_insert return manager._insert( File "C:\Users\Anaconda3\lib\site-packages\django\db\models\manager.py", line 85, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "C:\Users\Anaconda3\lib\site-packages\django\db\models\query.py", line 1791, in _insert return query.get_compiler(using=using).execute_sql(returning_fields) File "C:\Users\Anaconda3\lib\site-packages\django\db\models\sql\compiler.py", line 1660, in execute_sql cursor.execute(sql, params) File "C:\Users\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 103, in execute return super().execute(sql, params) File "C:\Users\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 67, in execute return self._execute_with_wrappers( File "C:\Users\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 80, in _execute_with_wrappers return executor(sql, params, many, context) File "C:\Users\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 89, in _execute return self.cursor.execute(sql, params) File "C:\Users\Anaconda3\lib\site-packages\django\db\utils.py", line 91, in exit raise dj_exc_value.with_traceback(traceback) from exc_value File "C:\Users\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 89, in _execute return self.cursor.execute(sql, params) File "C:\Users\Anaconda3\lib\site-packages\django\db\backends\mysql\base.py", line 75, in execute return self.cursor.execute(query, args) File "C:\Users\Anaconda3\lib\site-packages\MySQLdb\cursors.py", line 206, in execute res = self._query(query) File "C:\Users\Anaconda3\lib\site-packages\MySQLdb\cursors.py", line 319, in _query db.query(q) File "C:\Users\Anaconda3\lib\site-packages\MySQLdb\connections.py", line 254, in query _mysql.connection.query(self, query) django.db.utils.IntegrityError: (1062, "Duplicate entry 'admin1' for key 'username'")

I have got saved Custom User but Don't saving in palform but why is not showing database palform page. what was the problem? Can anyone help me?

The error says user with username 'admin1' already exists.Please try with another username.

The error says user with username 'admin1' already exists.Please try with another username.

Back to Top