New class dont sync with panel admin django - python

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)

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.

New model field is not showing in admin page on PythonAnywhere

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.

Django admin page not showing user models

My admin page is working fine except when logged in it is not showing any user models. It is hindering my work as I cannot manage users.
I have made custom models as shown below.
Database is MySQL.
models.py
class User(AbstractUser):
is_customer = models.BooleanField(default=False)
is_restaurant = models.BooleanField(default=False)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
class Customer(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
food_pref = models.CharField(max_length=10, default='veg')
class Restaurant(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
restaurant_name = models.CharField(max_length=100, blank=False)
Regisrar your models inadmin.py file.
from . models import Model_Name
Then you can register your models in two ways:
I) admin.site.register(Model_Name)
II)
#admin.register(Model_Name)
Class Xyz(admin.ModelAdmin):
pass
Second method gives you more flexibility like list_display, list_filter, date_hierarchy, etc. for customising your Admin section/site.
You can look more about customising admin site at https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#module-django.contrib.admin
Have You registered them in admin.py?
from .models import ModelName
admin.site.register(ModelName)

How to perform Update, Delete operation in Django rest framework

I want to perform Update and Delete operation in Django rest framework, I did Get and Post operation. I'm new to django, Please help me to do Update and Delete operation.
views.py
class StudentViews(viewsets.ModelViewSet):
queryset = Student.objects.all()
serializer_class = StudentSerializer
models.py
class Student(models.Model):
name = models.CharField(max_length=255, blank=True, null=True)
contact_number = models.CharField(max_length=12, blank=True, null=True)
email = models.EmailField(max_length=100, blank=True, null=True)
address = models.CharField(max_length=500, blank=True, null=True)
serializers.py
class StudentSerializer(serializers.ModelSerializer):
class Meta:
model = Student
fields = '__all__'
urls.py
router = routers.DefaultRouter()
router.register('api/student', views.StudentViews)
urlpatterns = [
path('', include(router.urls)),
]
You can do those operations(PUT,PATCH and DELETE) in the api/student/1234/ end-point, where the 1234 is the PK of the instance to be deleted or updated.
You can refer more related to the end-point which is created buy the router automatically here, DefaultRouter--[DRF-DOC]

How do I delete a field from my Django ModelForm?

I'm trying to hide and delete two fields from showing in a form I created in the Django administration page using ModelForm.
I looked at answers that said I should use the "exclude" meta field, but I don't know why it's not working in my case.
Here is my code:
models.py:
class Activity(models.Model):
type = models.CharField(max_length=50, default="")
title = models.CharField(max_length=200, default="")
description = models.CharField(max_length=500)
owner = models.ForeignKey(User, related_name="owner")
college = models.CharField(max_length=200)
location = models.CharField(max_length=200)
room = models.CharField(max_length=200)
startDate = models.DateTimeField(null=True, blank=True)
endDate = models.DateTimeField(null=True, blank=True)
attendee = models.ManyToManyField(Attendee, related_name="attendees",null=True, blank=True)
volunteer = models.ManyToManyField(Volunteer, related_name="volunteers",null=True, blank=True)
I'm trying to exclude the "attendee & volunteer" fields from displaying in the Django administration form.
In admin.py I have:
from django.contrib import admin
from django import forms
from KSUvity.models import Activity
class ActivityForm(forms.ModelForm):
class Meta:
model = Activity
exclude = ['attendee', 'volunteer',]
class ActivityAdmin(admin.ModelAdmin):
exclude = ['attendee', 'volunteer',]
form = ActivityForm
admin.site.register(Activity, ActivityAdmin)
You have to create an admin.py file in your app and register your models
Follow the instuctions
See the example below
from django import forms
from django.contrib import admin
from myapp.models import Person
class PersonForm(forms.ModelForm):
class Meta:
model = Person
exclude = ['name']
class PersonAdmin(admin.ModelAdmin):
exclude = ['age']
form = PersonForm
admin.site.register(Person, PersonAdmin)
You can use either fields or exclude in one class.
In your app admin field add this code.
app_name/admin.py
from django.contrib import admin
class ActivityAdmin(admin.ModelAdmin):
exclude = ('attendee', 'volunteer',)
You have to use ModelAdmin option to exclude fields from form in Django administration, either ModelAdmin.exclude or ModelAdmin.fields. Below is an example:
class ActivityAdmin(admin.ModelAdmin):
exclude = ('attendee', 'volunteer', )
To make it work, you register model like this:
admin.site.register(Activity, ActivityAdmin)
You add this code to admin.py file.

Categories