#Django foreign key error

9 messages · Page 1 of 1 (latest)

graceful creek
#

can you post some code of your model and your view ?

remote nexus
#
from django.shortcuts import render
from .models import Bot
from accounts.models import CustomUser
# Create your views here.

def register_bot(request):
    if request.method == "POST":
        client_id = request.POST.get('client_id')
        client_secret = request.POST.get("client_secret")
        password = request.POST.get('pas')
        username = request.POST.get('Username')
        user = CustomUser.objects.get(email=request.POST.get("email"))
        # Bot.object
        context = {"username": username}
        return render(request, "social_app/add_bot.html", context=context)
    return render(request, "accounts/register.html")```
#
from django.shortcuts import render
from .models import CustomUser
import praw

# Create your views here.


def register(request):
    if request.method == "POST":
        email = request.POST.get('email')
        password = request.POST.get("password1")
        user = CustomUser.objects.create(email=email,password=password)
        context = {"email":email}
        return render(request, "social_app/add_bot.html",context=context)
    return render(request,"accounts/register.html")

def login(request):
    context = {}
    return render(request,"accounts/login.html",context=context)
#
from accounts.models import CustomUser

class Bot(models.Model):
    botname = models.CharField(max_length=150)
    user = models.ForeignKey(CustomUser,on_delete=models.CASCADE)
    status = models.BooleanField(default=False)
    api_key = models.CharField(null=True,max_length=500)
    user_agent = models.BooleanField(blank=True)
    username = models.CharField(max_length=75)
    client_id = models.CharField(max_length=750)
    client_secret = models.CharField(max_length=750)
    pas = models.CharField(max_length=50)

#
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
from django.utils.translation import gettext_lazy as _
from django.utils import timezone
from .managers import CustomUserManager


class CustomUser(AbstractBaseUser,PermissionsMixin):
    email = models.EmailField(_("email address"), unique=True)
    password = models.CharField(max_length=128)
    is_active = models.BooleanField(default=True)
    date_joined = models.DateTimeField(default=timezone.now)
    first_name = models.CharField(max_length=30, blank=True)
    last_name = models.CharField(max_length=30, blank=True)
    is_staff = models.BooleanField(default=False)

    USERNAME_FIELD = "email"
    REQUIRED_FIELDS = []

    objects = CustomUserManager()

    def __str__(self):
        return self.email```
graceful creek
#

i want the traceback please

remote nexus
#

with self.db.wrap_database_errors:
File "C:\Users\FAIQ\PycharmProjects\social_media_app.venv\Lib\site-packages\django\db\utils.py", line 91, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\FAIQ\PycharmProjects\social_media_app.venv\Lib\site-packages\django\db\backends\utils.py", line 105, in _execute
return self.cursor.execute(sql, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\FAIQ\PycharmProjects\social_media_app.venv\Lib\site-packages\django\db\backends\sqlite3\base.py", line 354, in execute
return super().execute(query, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
django.db.utils.IntegrityError: NOT NULL constraint failed: accounts_customuser.password
[21/Nov/2024 21:40:42] "POST /register/ HTTP/1.1" 500 189430

graceful creek
#

You have to Check the name of your form input you want to fer the pas item but it doesn’t recognise it

remote nexus
#

what ??