lack of foreign key in admin model? - python

I get the following error when trying to update my database:
class 'artdb.admin.RoleInline': (admin.E202) 'artdb.Role' has no ForeignKey to 'artdb.Person'
I want ot have a many to many relation between Person and Role
model.py (not showing all classes):
class Person(models.Model):
mail=models.EmailField()
firstName=models.CharField(max_length=200)
lastName=models.CharField(max_length=200)
phoneNumber=PhoneNumberField()
streetAdress=models.CharField(max_length=200)
zipcode=models.CharField(max_length=200)
city=models.CharField(max_length=200,default="Göteborg")
country=models.CharField(max_length=200,default="Sweden")
def __str__(self):
return "%s %s" % (self.firstName,self.lastName)
class Meta:
ordering = ('firstName','lastName')
class Role(models.Model):
role=models.CharField(max_length=200)
person=models.ManyToManyField(Person)
def __str__(self):
return self.role
class Meta:
ordering = ('role',)
admin.py (not showing all classes):
from django.contrib import admin
from .models import Role
from .models import Address
from .models import Date
from .models import Person
from .models import Name
# Register your models here.
admin.site.register(Role)
admin.site.register(Address)
admin.site.register(Date)
admin.site.register(Name)
admin.site.register(Person)
class RoleInline(admin.TabularInline):
model=Role
extra=3
class PersonInline(admin.ModelAdmin):
fieldsets=[
(None,{'fields': ['mail','firstName','lastName','phoneNumber','streetAdress','zipcode','city','country']}),
]
inlines = [RoleInline]
search_fields = ['firstName']
#admin.site.register(Name,NameInline)
admin.site.register(Person,PersonInline)
it has worked before with run manage.py makemigrations artdb
I don't see the errorin the models.

You are having wrong model for RoleInline as per documentation for ManytoMany relationship in TabularInline
model = Role.persons.through

You need to use model=Role.person.through instead of model=Role as shown below:
class RoleInline(admin.TabularInline):
# model=Role
model=Role.person.through # Here
extra=3

Related

django graphene how to get a list of fields from the model.py

I'm using graphql together with django. My model.py might look like:
from django.db import models
class MyModel(models.Model):
field_a = models.CharField(max_length=100)
field_b = models.CharField(max_length=100)
field_c = models.CharField(max_length=100)
How can I get a list of all fields in a different class (i.e. schema.py)?
from .models import MyModel
model_fields = MyModel.__dict__
does not give me a list of fields.
Thanks for a suggestion
Short answer:
model_fields = MyModel._meta.fields
Long answer:
You probably want to use in graphene's built in functionality if you want to handle different models without explicitly writing the fields out. You can use the graphene_django library to do this
So lets say you have this in models.py:
class ModelA(models.Model):
field_a = models.CharField(max_length=100)
class ModelB(models.Model):
field_b = models.CharField(max_length=100)
then in schema.py you probably want to do this:
import graphene
from graphene_django import DjangoObjectType
from .models import ModelA, ModelB
class ModelAType(DjangoObjectType):
class Meta:
model = ModelA
class ModelBType(DjangoObjectType):
class Meta:
model = ModelB
These will create a graphene object with your model fields copied over for you. You can even add other fields on:
class ModelBType(DjangoObjectType):
class Meta:
model = ModelB
model_a = graphene.Field(ModelA)
def resolve_model_a(self, info)
return ModelA.objects.filter(field_a=self.field_b)

how to change admin field name in django, python

I added one model Author in models.py file in my app and created model names for the author while I opened in admin panel it's showing as Author object(12) how can I change that?
I tried to add Unicode
class Author(models.Model):
author_name=models.CharField(max_length=300)
I want field name instead of Author object in the admin panel.
below i want change Author Object
Try This:
class Author(models.Model):
author_name=models.CharField(max_length=300)
def __str__(self):
return self.author_name
Follow what #dirkgroten said "Make it a habit to always override str for all your models"
Also You can use list_display method in your admin.py to achieve similar result. Create a admin class and use list_display to render fields of model in tabular format
Admin.py
from app.models import Artist #<-----Import you artist model
#admin.register(Artist) #<----- admin class should be just below this line
class ArtistAdmin(admin.ModelAdmin):
list_display = ["id", "author_name"]
Or you can also do this:
from app.models import Artist #<-----Import you artist model
class ArtistAdmin(admin.ModelAdmin):
list_display = ["id", "author_name"]
admin.site.register(Artist, ArtistAdmin) #<----register your class also
You can overrride __str__ method in django model class like that
class Author(models.Model):
author_name=models.CharField(max_length=300)
def __str__(self):
return self.author_name
Here is the example of overriding __str__ method for cases like yours.
class Language(models.Model):
language = models.CharField(max_length=32)
class Meta:
app_label = "languages"
ordering = ["-modified"]
def __str__(self):
return f"{self.language} (language {self.id})"

How to register inherited sub class in admin.py file in django?

Project Name : fusion
App Name : admin_lte
Python 3.7
Django 2
MySql
Question is "I want to register sub model in django admin-panel",when i write code for model registration in admin.py file that time occurred below error.
django.core.exceptions.ImproperlyConfigured: The model Device is abstract, so it cannot be registered with admin.
NOTE : I used multiple separated model file.
device.py (Model File)
from django.db import models
class Device(models.Model):
device_type = models.CharField(max_length=100,blank=False)
price = models.IntegerField()
status = models.CharField(max_length=10, default="SOLD")
issues = models.CharField(max_length=100, default="No Issues")
class Meta:
abstract = True
def __str__(self):
return 'Device_type:{0} Price:{1}'.format(self.device_type,self.price)
#Inheritance Concept
class Laptop(Device):
pass
class Meta:
db_table = "laptop"
class Desktop(Device):
pass
class Meta:
db_table = "Desktop"
class Mobile(Device):
pass
class Meta:
db_table = "Mobile"
__init__.py File
from django_adminlte.models.employee import Employee
from django_adminlte.models.device import Device
admin.py
from django.contrib import admin
from.models import Employee
from.models import Device
admin.site.register (Employee)
admin.site.register (Device)
I want to show sub model (Desktop,Laptop,Mobile) in admin panel so admin can add some data from admin panel.
Project Structure Image :
I can see in your code Device is a abstract model. So, we should not register it because abstract models do not have associated tables in databases.
from django.contrib import admin
from .models import Employee, Laptop, Mobile, Desktop
admin.site.register(Employee)
admin.site.register(Laptop)
admin.site.register(Mobile)
admin.site.register(Desktop)
I also got a problem when trying to generate an admin CRUD for a class inheriting from an abstract class. But the cause was different so I'll leave my case here in case it helps someone else.
In my case, the problem was that I forgot to make the abstract class inherit from django's models.Model.
Example Code:
time.py
from django.db import models
from applications.utils import UniqueNameMixin
class Month(UniqueNameMixin):
starting_date = models.DateField()
ending_date = models.DateField()
class TimeSensible(models.Model): # Here '(models.Model)' was missing.
class Meta:
abstract = True
month = models.ForeignKey(Month, models.PROTECT)
transaction.py
from django.db import models
from applications.core.models.cash_flow import Concept
from applications.core.models.financial_position import Account
from applications.core.models.time import TimeSensible
class Transaction(models.Model, TimeSensible):
concept = models.ForeignKey(Concept, models.PROTECT, blank=True, null=True)
amount = models.DecimalField(max_digits=10, decimal_places=2)
account = models.ForeignKey(Account, models.PROTECT)
detail = models.CharField(max_length=100, blank=True, null=True)
def __str__(self):
return '{} - {} - {} - {}'.format(self.month, self.concept, self.amount, self.account)
The error I got:
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: The model Transaction is abstract, so it cannot be registered with admin.

Why are my django model fields not working?

I don't think that it is recognizing the existence of my fields. Here's my models.py:
from django.db.models import *
from django.contrib import admin
from django.forms import *
class Stock(Model):
name = CharField(max_length=60)
class Meta:
ordering = ["name"]
def __unicode__(self):
return self.name
admin.site.register(Stock)
When I run it, I get this error: "portfolio.stock: "ordering" refers to "name", a field that doesn't exist." When I comment the meta function out and run it, it works fine until the admin site, where when I try to create a stock object, the fields don't show up.
I'm completely confused by what's going on.
The problem is your * imports.
django.db.models.CharField is being replaced by django.forms.CharField:
>>> from django.db.models import *
>>> CharField
<class 'django.db.models.fields.CharField'>
>>> from django.forms import *
>>> CharField
<class 'django.forms.fields.CharField'>
So, actually name = CharField(max_length=60) defines a form field instead of a model one - it breaks everything and makes this bug subtle.
Solution: remove unnecessary forms import and be explicit in your imports:
from django.db import models
from django.contrib import admin
class Stock(models.Model):
name = models.CharField(max_length=60)
class Meta:
ordering = ["name"]
def __unicode__(self):
return self.name
admin.site.register(Stock)

Django ModelAdmin extra input field not from Model

I'm trying to add an extra input to a admin.ModelAdmin for a model I have so I can record some optional text when another input has changed.
I can't get the custom ModelForm recognised as name 'EquipmentAdmin' is not defined. I've tried several different ways of importing but think I must have missed something obvious. It feels like there's a circular reference between the EquipmentAdmin and EquipmentAdminForm as they both include a reference to each other in the code.
I have created my Django application Flightdeck and have these all in the same folder;
models.py
from django.db import models
class Location(models.Model):
name = models.CharField(max_length=45)
class Equipment(models.Model):
unit_id = models.CharField(max_length=45)
located = models.ForeignKey(Location)
located_from = models.DateField()
class EquipmentLocationHistory(models.Model):
unit = models.ForeignKey(Equipment)
located = models.ForeignKey(Location)
start = models.DateField()
end = models.DateField()
change_reason = models.CharField(max_length=45)
admin.py
from django.contrib import admin
from flightdeck.models import *
from flightdeck.forms import EquipmentAdminForm
class EquipmentAdmin(admin.ModelAdmin):
form = EquipmentAdminForm
def save_model(self, request, obj, form, change):
if 'located' in form.changed_data:
try:
old = Equipment.objects.get(unit_id=obj.unit_id)
except Equipment.DoesNotExist:
old = None
if old:
if 'located' in form.changed_data:
located_history = EquipmentLocationHistory(unit=obj, located=old.located, start=old.located_from, end=obj.located_from)
located_history.save()
obj.save()
forms.py
from django import forms
from django.contrib import admin
class EquipmentAdminForm(forms.ModelForm):
reason = forms.CharField()
class Meta:
model = EquipmentAdmin
I would like to include the reason value when I add the EquipmentLocationHistory but can't test what I have as the EquipmentAdminForm isn't loaded.
EquipmentAdmin is not a model. Your ModelForm needs to reference Equipment
from django import forms
from django.contrib import admin
from flightdeck.models import Equipment
class EquipmentAdminForm(forms.ModelForm):
reason = forms.CharField()
class Meta:
model = Equipment
PS: when you have circular references, there are many ways around the problem. The best way with model imports and django is to use django.db.models.get_model('app', 'model')

Categories

Resources