I have the following scenario where there are three models as follows which should be displayed nested in DJango admin. Am using Django 1.6 release and applied settings as put up in https://github.com/Soaa-/django-nested-inlines
However, it didnt turn up with the expected output. Is there any other solutions to implement nested inlines in Django. I'm a newbie to this framework. Kindly guide me to fix this issue.
model.py
class Project(models.Model):
name = models.CharField(max_length=200)
code = models.IntegerField(default=0)
def __unicode__(self):
return self.name
class Detail(models.Model):
project = models.ForeignKey(Project)
value = models.DecimalField(max_digits=5, decimal_places=2)
location = models.IntegerField(default=0)
class Configuration(models.Model):
detail = models.OneToOneField(Detail)
content1 = models.IntegerField()
content2 = models.IntegerField()
admin.py
from django.contrib import admin
from nested_inlines.admin import NestedModelAdmin, NestedTabularInline, NestedStackedInline
from myapp.models import Project, Detail, Configuration
class ConfigInline(NestedStackedInline):
model = Configuration
extra = 1
class DetailInline(NestedTabularInline):
model = Detail
inlines = [ConfigInline,]
extra = 1
class ProjectAdmin(admin.ModelAdmin):
inlines = [DetailInline]
admin.site.register(Project, ProjectAdmin)
try https://pypi.python.org/pypi/django-nested-inline .
It has been updated to work with Django 1.6
I believe you've forgotten to set the ProjectAdmin as a NestedModelAdmin:
admin.py :
from django.contrib import admin
from nested_inlines.admin import NestedModelAdmin, NestedTabularInline, NestedStackedInline
from myapp.models import Project, Detail, Configuration
class ConfigInline(NestedStackedInline):
model = Configuration
extra = 1
class DetailInline(NestedTabularInline):
model = Detail
inlines = [ConfigInline,]
extra = 1
class ProjectAdmin(NestedModelAdmin):
inlines = [DetailInline]
admin.site.register(Project, ProjectAdmin)
Before you import this below package first install this
pip install django-nested-admin
then add this in installed app list
INSTALLED_APPS = [
...
'nested_admin',
...
]
Now, you can import it.
from nested_inline.admin import NestedModelAdmin, NestedTabularInline, NestedStackedInline
Related
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.
I using a nested model in a Django project.
The following snippet code is models.py:
from django.db import models
from django.db.models.deletion import CASCADE
class Model_(models.Model):
name = models.CharField(max_length=50, default="This is a model")
frequently = models.FloatField(default=1.0)
def __str__(self):
return self.name
class SubModel(models.Model):
name = models.CharField(max_length=100)
address = models.CharField(max_length=8, default='0x')
model_ = models.ForeignKey(Model_, on_delete=CASCADE)
def __str__(self):
return self.name
class Metadata(models.Model):
key = models.CharField(max_length=100)
value = models.CharField(max_length=100)
sub_model = models.ForeignKey(SubModel, on_delete=CASCADE)
This is my admin.py script:
from django.contrib import admin
from nested_inline.admin import NestedTabularInline, NestedStackedInline,\
NestedModelAdmin
from <djano-application-name>.models import Model_, SubModel, Metadata
class MetadataAdmin(NestedTabularInline):
model = Metadata
extra = 1
class SubModelAdmin(NestedStackedInline):
model = SubModel
inlines = [MetadataAdmin]
extra = 1
class Model_Admin(NestedModelAdmin):
model = Model_
inlines = [SubModelAdmin]
list_display = ['name']
admin.site.register(Model_, Model_Admin)
Question:
What is the difference between NestedStackedInline and NestedTabularInline in admin.py script?
[NOTE]:
Versions: Python 2.7 and Django 1.11
If you are using django-nested-inline, It means you wanted to edit models on the same page as a parent model and add more than 1 level of children at once with the parent object in admin.
The Django admin is just a normal Django application and you can't have a second level of inlines(nested forms) in the default Django admin.
The difference between NestedStackedInline and NestedTabularInline is just Layout. Indeed, both work exactly the same behind the scenes, the only difference is the template used for rendering. Check the official docs. So, picking one for your project is only a matter of preference regarding the interface layout.
This is how NestedStackedInline will look, each field of the model is under other.
and this is NestedTabularInline, each field of the model is in one line, column wise
This is my models
from django.db import models
class Page(models.Model):
page_id = models.IntegerField(default=0)
class Question(models.Model):
page = models.ForeignKey(Page)
question = models.CharField(max_length=150)
class Option(models.Model):
question = models.ForeignKey(Question)
option = models.CharField(max_length=100)
image_class = models.CharField(max_length=75)
this is my admin.py
from django.contrib import admin
from .models import Page, Question, Option
class OptionInline(admin.StackedInline):
model = Option
extra = 1
class QuestionInline(admin.StackedInline):
model = Question
extra = 1
inlines = [OptionInline]
class PageAdmin(admin.ModelAdmin):
inlines = [QuestionInline]
admin.site.register(Page, PageAdmin)
basically i want this multi level relation to appear as a multi level inline in the admin site. can someone please help out
Instead of using nested inlines, Django 1.8 provides the InlineModelAdmin.show_change_link
from django.contrib import admin
from .models import Page, Question, Option
class OptionInline(admin.StackedInline):
model = Option
extra = 1
class QuestionInline(admin.StackedInline):
model = Question
extra = 1
show_change_link = True
class PageAdmin(admin.ModelAdmin):
inlines = [QuestionInline,]
admin.site.register(Page, PageAdmin)
class QuestionAdmin((admin.ModelAdmin):
inlines = [OptionInline,]
admin.site.register(Question, QuestionAdmin)
This way, when you save the Page model -having completed the inline Question model- a link called 'change' will appear at the saved instance of the inline Question model. Clicking it, you will land at the main page of the Question model instance with the Option model as inline.
When you complete the Option model inline and hit the 'save and continue editing', the back button should return you to the relevant Page instance.
There is also a post which describes how you can achieve the same result if you use previous Django versions.
Django does not support it out of the box, but there is project called django-nested-inline that will do the job. Also you can make your own solution.
Building a generic app to practice learning with Django.
Two classes in Models:
class HouseInformation(models.Model):
house_name = models.CharField(max_length=200)
house_type = models.CharField(max_length=40)
address = models.CharField(max_length=200)
latitude = models.CharField(max_length=200)
longitude = models.CharField(max_length=200)
def __str__(self):
return self.house_name
class HouseReport(models.Model):
the_house = models.ForeignKey(HouseInformation)
visit_date = models.DateField()
In Admin view, I'd like to see a list of the houses with the dates they were visited. The admin.py so far is like so, and its not working:
from django.contrib import admin
from housing.models import HouseInformation
from housing.models import HouseReport
class HouseReport(admin.ModelAdmin)
list_display = ('the_house')
admin.site.register(HouseInformation, HouseReport)
I hope the one-to-many is represented correctly (one house can have many visits).
The problem is the missing ::
class HouseReport(admin.ModelAdmin):
^
Speaking about the task you've initially wanted to solve, check the InlineModelAdmin classes:
The admin interface has the ability to edit models on the same page as
a parent model. These are called inlines.
Add this to the admin.py:
from django.contrib import admin
from housing.models import HouseInformation, HouseReport
class HouseReportInline(admin.TabularInline):
model = HouseReport
class HouseAdmin(admin.ModelAdmin):
inlines = [
HouseReportInline,
]
admin.site.register(HouseInformation, HouseAdmin)
And you will see the House information and all of the HouseReports associated with a House on the House admin page.
You forgot the : after the class definition in line 5
class HouseReport(admin.ModelAdmin):
And you have to write
...
list_display = ('the_house',)
...
notice the trailing comma? It tells python, that it should create a tuple
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')