Django FileNotFoundError at /admin - python

I have model registered on admin page:
models.py
from django.db import models
class Question(models.Model):
cat = models.IntegerField()
quest = models.TextField(max_length=200)
answer = models.CharField(max_length=1000)
path = models.FilePathField()
date = models.DateTimeField(auto_now_add=True)
status = models.IntegerField(default=0)
def __str__(self):
return f'{self.cat} - {self.quest}'
admin.py
from django.contrib import admin
from .models import Question
admin.site.register(Question)
and I can see a database through admin page:
https://i.stack.imgur.com/SuCcX.png
but I can't click on any record of the table and modify it due to an error:
https://i.stack.imgur.com/M6W5a.png
I did it many times since now, and I have never encountered such an error.
Does anybody have an idea how to fix it?

Acorrding to Django docs FilePathField() has one required argument path, which is:
"The absolute filesystem path to a directory from which this FilePathField should get its choices."
So you need modify your models.py:
class Question(models.Model):
...
path = models.FilePathField(path='/home/images')
...

Related

The model is not displayed in the django admin panel

I don't have the advertisement module displayed in the django admin panel. Here is the model code
from django.db import models
class Advertisement(models.Model):
title = models.CharField(max_length=1000, db_index=True)
description = models.CharField(max_length=1000, default='', verbose_name='description')
creates_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
price = models.FloatField(default=0, verbose_name="price")
views_count = models.IntegerField(default=1, verbose_name="views count")
status = models.ForeignKey('AdvertisementStatus', default=None, null=True, on_delete=models.CASCADE,
related_name='advertisements')
def __str__(self):
return self.title
class Meta:
db_table = 'advertisements'
ordering = ['title']
class AdvertisementStatus(models.Model):
name = models.CharField(max_length=100)
admin.py /
from django.contrib import admin
from .models import Advertisement
admin.site.register(Advertisement)
I was just taking a free course from YouTube. This was not the case in my other projects. Here I registered the application got the name in INSTALLED_APPS. Then I performed the creation of migrations and the migrations themselves. Then I tried to use the solution to the problem here , nothing helped. I didn't find a solution in Google search either.
127.0.0.1:8000/admin/
console
admins.py
The name of the file is admin.py not admins.py. Yes, that is a bit confusing since most module names in Django are plural. The rationale is probably that you define a (single) admin for the models defined.
Alternatively, you can probably force Django to import this with the AppConfig:
# app_name/apps.py
from django.apps import AppConfig
class AppConfig(AppConfig):
def ready(self):
# if admin definitions are not defined in admin.py
import app_name.admins # noqa

Django Operation Error after saved objects

This keep popping up on web after save an object, not sure what happens: screenshot
OperationalError at /admin/product/product/add/
no such table: product_product
Request Method: POST
Request URL: http://127.0.0.1:8000/admin/product/product/add/
Django Version: 3.1.2
Exception Type: OperationalError
Exception Value:
no such table: product_product
My code:
admin.py
from django.contrib import admin
# Register your models here.
from .models import product
admin.site.register(product)
models.py
from django.db import models
# Create your models here.
class product(models.Model):
title = models.CharField(max_length=222)
description = models.TextField(blank=True, null=True)
price = models.DecimalField(max_digits=333, decimal_places=2)
summary = models.TextField(default = 'this is cool!')
feature = models.BooleanField()
apps.py
from django.apps import AppConfig
class ProductConfig(AppConfig):
name = 'product'
I found out it's migration problem, if it states No changes detected, it doesn't mean no change needed to be made, as what I missed is the app file name typed after makemigrations in terminal.

AttributeError at /admin/student/student_record/

I updated my django model, for my student app, it now looks like this
from django.db import models
class Student_Record(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
email = models.CharField(max_length=50)
address = models.CharField(max_length=500)
gpa = models.FloatField()
def __str__(self):
return str(self.student_id) + ", " + self.first_name
there used to be an IntegerField, student_id but I removed it. I made the migrations, and everything runs fine, but when I visit the django admin panel, to manually update the database records, I get the following error
does anyone know what's going on?
I registered the model, in admin.py
from django.contrib import admin
from .models import Student_Record
admin.site.register(Student_Record)
Look at your __str__ method it still references student_id.
You should search for the field name in your whole project, there might be other occurrences elsewhere.

NameError at / name 'article_finish_date' is not defined

So I am trying to create a custom Manager that extends the default one and I am getting an error that for the life of me I cannot fix. I've read all the django docs and can't see what I've done wrong!
ERROR:
NameError at /
name 'article_finish_date' is not defined
Here is my models.py
import datetime
from django.conf import settings
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
class ArticleManager(models.Manager):
def get_queryset(self):
return super(ArticleManager, self).get_queryset().filter(article_finish_date==None)
class Article(models.Model):
article_name_text = models.CharField(max_length=200)
article_creation_date = models.DateTimeField('date created')
article_publish_date = models.DateTimeField('date published', null=True, blank=True)
article_finish_date = models.DateTimeField('date finished', null=True, blank=True)
def __str__(self):
return self.article_name_text
actives = ArticleManager()
I have tried filtering by all the different values of the Article model, however the same issue occurs. I have tried both migrated and makemigrations, however no progress has been made.
Many thanks in advance, ask if you need more details!
The error is in this part:
.filter(article_finish_date==None)
Python is trying to evaluate the expression article_finish_date==None, except article_finish_date hasn't been defined yet. Instead, you want to use a single =
.filter(article_finish_date=None)

Foreign key constraint error on Django app

I'm trying to have this third class noticeTime be constrained to the foreign key email. I am using the same syntax that worked for the 2nd class location, but when I use it on noticeTime it throws an error:
Exception Value: no such column: setupNotifications_noticetime.email_id
Here is the code:
from django.db import models
# Create your models here.
from django.db import models
class email(models.Model):
email = models.CharField(max_length=200)
def __unicode__(self):
return self.email`
class location(models.Model):
email = models.ForeignKey(email)
zip_code = models.CharField(max_length=5)
def __unicode__(self):
return self.zip_code
class noticeTime(models.Model):
email = models.ForeignKey(email)
time = models.CharField(max_length=200)
def __unicode__(self):
return self.time
here is admin.py:
from django.contrib import admin
# Register your models here.
from setupNotifications.models import email
from setupNotifications.models import location
from setupNotifications.models import noticeTime
admin.site.register(email)
admin.site.register(location)
admin.site.register(noticeTime)
I'm using the sqlite database
Perhaps your problem is that you ran syncdb, assuming that it would alter the table to match your model change. Unfortunately, it does not do that. There are some separate tools available, such as South, which can help with database migrations.

Categories