New model field is not showing in admin page on PythonAnywhere - python

I have a problem when adding a new field to my models.py in PythonAnywhere. My models.py looks like the following, and I recently added the description field to it.
class Post(models.Model):
title = models.CharField(max_length=40, unique=True)
cover = models.ImageField(upload_to='images/')
description = models.CharField(max_length=100)
author = models.ForeignKey(User, on_delete= models.CASCADE,related_name='blog_posts')
slug = models.SlugField(null=False, unique=True)
def get_absolute_url(self):
return reverse('post_detail', kwargs={'slug': self.slug})
updated_on = models.DateTimeField(auto_now= True)
shortcontent = models.CharField(max_length=150)
content = RichTextUploadingField(blank=True)
created_on = models.DateTimeField(auto_now_add=True)
language = models.IntegerField(choices=LANGUAGE, default=0)
status = models.IntegerField(choices=STATUS, default=0)
class Meta:
ordering = ['-created_on']
def __str__(self):
return self.title
I did run the makemigrations and python manage.py migrate commands, but the description field is not showing on my admin page:
My admin.py looks like this:
class PostAdmin(admin.ModelAdmin):
list_display = ('title', 'status','created_on')
list_filter = ("status",)
search_fields = ['title', 'content', 'description']
prepopulated_fields = {'slug': ('title',)}
class Media:
js = ('ckeditor.js',)
# do not write '/static/ckeditor.js' as Django automatically looks
# in the static folder
admin.site.register(Post, PostAdmin)
What can I do to make sure the description field shows up on my admin page. I am using Python 3.8 in Pythonanywhere.

The problem gets solved if I reload the web app in the 'web tab' of PythonAnywhere.

Related

'project.Account' has no ForeignKey to 'project.Object': How to link an account model to the objects of a project?

I am trying to create an announcement website (All) that can be visible to others (the Users, for which I added an Account). For this I wanted to modify a little the user profile to add fields like telephone, email address...
So I modified admin.py:
from django.contrib import admin
from .models import Todo, Account
from django.contrib.auth.models import User
class AccountInline(admin.StackedInline):
model = Account
can_delete = False
verbose_name_plural = 'Accounts'
class TodoAdmin(admin.ModelAdmin):
readonly_fields = ('created',)
inlines = (AccountInline, )
admin.site.unregister(User)
admin.site.register(Todo, TodoAdmin)
But got back:
<class 'todo.admin.AccountInline'>: (admin.E202) 'todo.Account' has no ForeignKey to 'todo.Todo'.
So I added a ForeignKey to Todo with account = models.ForeignKey(Account, on_delete=models.CASCADE):
from django.db import models
from django.contrib.auth.models import User
class Account(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
email = models.CharField(max_length=100)
firstname = models.CharField(max_length=30)
lastname = models.CharField(max_length=50)
company = models.CharField(max_length=5)
def __str__(self):
return self.user.username
class Todo(models.Model):
title = models.CharField(max_length=100)
datetime = models.DateTimeField()
memo = models.TextField(blank=True)
created = models.DateTimeField(auto_now_add=True)
datecompleted = models.DateTimeField(null=True, blank=True)
important = models.BooleanField(default=False)
user = models.ForeignKey(User, on_delete=models.CASCADE)
account = models.ForeignKey(Account, on_delete=models.CASCADE)
def __str__(self):
return self.title
But I still have the error, and I don't have any Users in the admin panel anymore
You accidentally wrote unregister for Users in your admin.py file. It should be admin.site.register(User)
You misinterpretted the error: the error states that you don't have a foreign key in your Account model to Todo.
This means your inline admin code isn't correct as it's expecting the other way around.

Creating a unique (attribute) title in a Model

I have a blog project and users can create posts with similar titles, How can I prevent a user or even the admin from proceeding without getting an error that the title already exists so that I can avoid future errors in the website such as get() returned more than one Post-it returned 2!
I have tried to use class meta for unique together but still, post was saved with the same title
Here is the post model
class Post(models.Model):
designer = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
slug = models.SlugField(blank=True, null=True, max_length=120)
def __str__(self):
return self.title
class Meta:
unique_together = ('title', 'slug')
You can add unique=True
title = models.CharField(max_length=100, unique=True)
See the docs here.

Makemigrations error on model.py

I am new on Python and Django. While trying out an example on a book, I did what the book says and created a models file as follows
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
class Post(models.Model):
STATUS_CHOICES = (
('draft','Draft'),
('published','Published'),
)
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250,
unique_for_date='published')
author = models.ForeignKey(User,
related_name='blog_posts')
body = models.TextField()
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10,
choices = STATUS_CHOICES,
default='draft')
class Meta:
ordering = ('-publish',)
def __str__(self):
return self.title
When I run the command, python manage.py makemigrations blog
Its says NameError: name 'STATUS_CHOICES' is not defined
I typed exactly as it is from the book and I am unable to run this command
try to indent your code,you must respect coding style:
status = models.CharField(max_length=10,
choices=STATUS_CHOICES,
default='draft')
Change
choices = STATUS_CHOICES,
to
choices = 'STATUS_CHOICES',
You are missing ''.
This is the problem of indent and spacing ,anyways after adding default in publish field and code formatting it worked.
The model.py is hosted here and DONOT edit or open in notepad like editor otherwise it will again create spacing, just import in your IDE

Abstract model error

I have problem about the abstract model in chapter "Adding Easy Admin Emails, Helpers, Sitemaps, and More" (the hello web app intermediate book)
here is my models.py code:
from django.contrib.auth.models import User
from django.db import models
class Timestamp(models.Model):
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
class Thing(Timestamp):
name = models.CharField(max_length=225)
class Thing(models.Model):
name = models.CharField(max_length=225)
description = models.TextField()
slug = models.SlugField(unique=True)
user = models.OneToOneField(User, blank=True, null=True)
def get_absolute_url(self):
return "/things/%s/" % self.slug
class Social(models.Model):
SOCIAL_TYPES = (
('twitter','Twitter'),
('facebook','Facebook'),
('pinterest','Pinterest'),
('instagram','Instagram'),
)
network = models.CharField(max_length=255, choices=SOCIAL_TYPES)
username = models.CharField(max_length=255)
thing = models.ForeignKey(Thing,related_name="social_accounts")
class Meta:
verbose_name_plural = "Social media links"
when I run
$python manage.py makemigrations
$python manage.py migrate
the error message shows:
/helloApp/venv/lib/python2.7/site-packages/django/db/models/base.py:309: RuntimeWarning: Model 'collection.thing' was already registered. Reloading models is not advised as it can lead to inconsistencies, most notably with related models.
new_class._meta.apps.register_model(new_class._meta.app_label, new_class)
No changes detected
that does not show like in the book :
You are trying to add a non-nullable field 'added' to thing without a defaul,,,,,,,
any answer would very greatful thank you!

New class dont sync with panel admin django

i just defined new class for my project. When I run server i dont recive errors just dont appears on the admin-django Panel.
Here is the new class from models.py
class Stock_Clinicas(models.Model):
organization = models.ForeignKey(Organization, null=True, blank=True, on_delete=models.SET_NULL)
products = models.ForeignKey(Products, null=True, blank=True, on_delete=models.SET_NULL)
provider = models.ForeignKey(Provider, null=True, blank=True, on_delete=models.SET_NULL)
stock_min = models.DecimalField('Stock minimo', max_digits=10, decimal_places=0)
stock_opt = models.DecimalField('Stock optimo', max_digits=10, decimal_places=0)
stock_act = models.DecimalField('Stock actual', max_digits=10, decimal_places=0, default=0)
and here my admin.py. i'll put all code.
from django.contrib import admin
from stock.models import *
from .models import Organization, OrganizationUser, OrganizationOwner
class OwnerInline(admin.StackedInline):
model = OrganizationOwner
raw_id_fields = ('organization_user',)
class OrganizationAdmin(admin.ModelAdmin):
inlines = [OwnerInline]
list_display = ['name', 'is_active']
prepopulated_fields = {"slug": ("name",)}
class OrganizationUserAdmin(admin.ModelAdmin):
list_display = ['user', 'organization', 'is_admin']
raw_id_fields = ('user', 'organization')
class OrganizationOwnerAdmin(admin.ModelAdmin):
raw_id_fields = ('organization_user', 'organization')
class Stock_ClinicasAdmin(admin.ModelAdmin):
list_display = ('stock_min', 'stock_opt', 'stock_act')
admin.site.register(Stock_Clinicas, Stock_ClinicasAdmin)
admin.site.register(Organization, OrganizationAdmin)
admin.site.register(OrganizationUser, OrganizationUserAdmin)
admin.site.register(OrganizationOwner, OrganizationOwnerAdmin)
Forget said, when I use syncdb the tables aren't created. I'm using Django 1.5.
dont resolved yet
You have a few issues:
class Stock_ClinicasAdmin(admin.ModelAdmin):
list_display = ('Stock Minimo', 'Stock Actual', 'Stock Optimo')
You don't have any fields called Stock Minimo, the field is stock_min, so you need to adjust this.
If you just created this app, make sure:
You add it to INSTALLED_APPS; it should have stock,
You might want to run manage.py makemigrations and manage.py migrate to create the tables.
In admin.py, rename your modelAdmin class to Stock_ClinicasAdmin (or anything else that your model name), and add this after the modelAdmin class declaration:
admin.register(Stoc_Clinicas, Stock_ClinicasAdmin)

Categories