I am following the official docs of Djongo mapper => https://www.djongomapper.com/using-django-with-mongodb-array-field/ to add Array Fields to my Model, but unfortunately even after adding the Array Fields as stated in the docs I am unable to see them in the below view shown in the docs.
Here is the Model defined by me.
from djongo import models
from django import forms
# Create your models here.
class Author(models.Model):
name = models.CharField(max_length=200)
email = models.EmailField()
class Meta:
abstract = True
def __str__(self):
return self.name
class EventModel(models.Model):
_id = models.ObjectIdField()
authors = models.ArrayField(
model_container=Author,
)
def __str__(self):
return self._id
Here is the generated Django Admin view based on my Models, which is no way similar to the one shown in docs here.
https://www.djongomapper.com/using-django-with-mongodb-array-field/
Any help on this would be appreciated thanks :)
Related
I am looking for a way to implement the "add new model_name" functionality from Django admin to normal form in templates, i.e; outside of Django admin, how could I use the same functionality.
How could I achieve it?
The first Step is to create Business Module inside models.py
class Business(models.Model):
name = models.CharField(max_length=200, db_index=True)
slug = models.SlugField(max_length=200, db_index=True, unique=True)
class Meta:
ordering = ('name',)
verbose_name = 'business'
verbose_name_plural = 'business'
def __str__(self):
return self.name
Then use python manage.py migrate to migrate module inside your database.
Now open admin.py file and register this Module,
from .models import Business
# Register your models here.
class BusinessAdmin(admin.ModelAdmin):
list_display = ['name', 'slug']
prepopulated_fields = {'slug': ('name',)}
admin.site.register(Business,BusinessAdmin)
Now check your Django admin panel. It will show you New Business Module there with Add, remove feature using Form.
I hope this will helpful for you.
I have a Django project hosted on heroku
I added a new slug field to model
from django.db import models
class Category(models.Model):
name = models.CharField(max_length=30)
slug = models.SlugField(unique=True)
def __unicode__(self):
return self.name
migrated it using south on heroku. Checked the heroku postgresDB as well for added field. All fine.
Opened Admin. No slug field showing...
added slug to fields[] in admin.py. Still not showing. Here is admin.py
from django.contrib import admin
from models import Category
class CategoryAdmin(admin.ModelAdmin):
fields = ('name', 'slug')
admin.site.register(Category, CategoryAdmin).
I even did a heroku restart... No change.
What can be done to show it ?
Try to use list_display like following:
from django.contrib import admin
from models import Category
class CategoryAdmin(admin.ModelAdmin):
fields = ('name', 'slug')
#list of fields to display in django admin
list_display = ['id', 'name', 'slug']
#if you want django admin to show the search bar, just add this line
search_fields = ['name', 'slug']
#to define model data list ordering
ordering = ('id','name')
admin.site.register(Category, CategoryAdmin).
Just in case someone ever faces this scenario
My admin classes were inheriting from UserAdmin, when they should have been inheriting from admin.ModelAdmin.
I had to change
class Model1(UserAdmin):
....
to
class Model1(admin.ModelAdmin):
....
I see the solution in here Django Website: https://docs.djangoproject.com/en/3.2/ref/models/fields/#editable, use fields's editable property.
editable
Field.editable
If False, the field will not be displayed in
the admin or any other ModelForm. They are also skipped during model
validation. Default is True.
I have posted a png image before, but I don't know how to display it.
I have the following in my models.py:
from django.db import models
class LabName(models.Model):
labsname=models.CharField(max_length=30)
def __unicode__(self):
return self.labsname
class ComponentDescription(models.Model):
lab_Title=models.ForeignKey('Labname')
component_Name = models.CharField(max_length=30)
description = models.CharField(max_length=20)
purchased_Date = models.DateField()
status = models.CharField(max_length=30)
to_Do = models.CharField(max_length=30,blank=True)
remarks = models.CharField(max_length=30)
def __unicode__(self):
return self.component
I have the following in my admin.py:
from django.contrib import admin
from Lab_inventory.models import ComponentDescription,LabName
class ComponentDescriptionAdmin(admin.ModelAdmin):
list_display= ('lab_Title','component_Name','description','purchased_Date','status','to_Do','remarks')
list_filter=('lab_Title','status','purchased_Date')
admin.site.register(LabName)
admin.site.register(ComponentDescription,ComponentDescriptionAdmin)
What I want is to display the fields under the component description to be displayed under the lab title(the fields related to each lab title by should be displayed under that lab name)
What you are doing with list_display and list_filter pertain to the list that is shown in the admin screen where the list of LabName objects are listed.
Assuming one LabName has one-to-many ComponentDescription entities, you need Django's InlineModelAdmin to display the list of ComponentDescription objects belonging to LabName within the admin page for a specific LabName entity. The code would be of the following structure:
from django.contrib import admin
from Lab_inventory.models import ComponentDescription,LabName
class ComponentDescriptionInline(admin.TabularInline):
model = ComponentDescription
class LabNameAdmin(admin.ModelAdmin):
inlines = [
ComponentDescriptionInline,
]
admin.site.register(LabName, LabNameAdmin)
where TabularInline is a subclass of the generic InlineModelAdmin.
In change list view in django admin interface, is it possible to mark some fields/rows red in if they achieve a expression?
For example, if there is a model Group with members and capacity, how can I visualize when they are full or crowded?
For modifying how and what is displayed in change list view, one can use list_display option of ModelAdmin.
Mind you, columns given in list_display that are not real database fields can not be used for sorting, so one needs to give Django admin a hint about which database field to actually use for sorting.
One does this by setting admin_order_field attribute to the callable used to wrap some value in HTML for example.
Example from Django docs for colorful fields:
class Person(models.Model):
first_name = models.CharField(max_length=50)
color_code = models.CharField(max_length=6)
def colored_first_name(self):
return '<span style="color: #%s;">%s</span>' % (
self.color_code, self.first_name)
colored_first_name.allow_tags = True
colored_first_name.admin_order_field = 'first_name'
class PersonAdmin(admin.ModelAdmin):
list_display = ('first_name', 'colored_first_name')
I hope some of this helps.
This is an old question but I'll add an example from docs for Django 1.10 because allow_tags attribute used in the accepted answer is deprecated since Django 1.9 and it is recommended to use format_html instead:
from django.db import models
from django.contrib import admin
from django.utils.html import format_html
class Person(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
color_code = models.CharField(max_length=6)
def colored_name(self):
return format_html(
'<span style="color: #{};">{} {}</span>',
self.color_code,
self.first_name,
self.last_name,
)
class PersonAdmin(admin.ModelAdmin):
list_display = ('first_name', 'last_name', 'colored_name')
In addition you can use
colored_first_name.short_description = 'first name'
For a nice column title
I have a legacy database with tables for documents and authors. A third table defines an ordered many to many relationship between the documents and authors, using foreign keys to the documents and the authors and an integer to specify the author order for a given document.
Using Django 1.1.1 (or SVN), is there a way to edit the document authors and their order in an admin page?
This is quick and a bit rough, but it should get you close(r).
from django.db import models
from django.contrib import admin
class Document(models.Model):
name = models.CharField(max_length = 128)
def __unicode__(self):
return self.name
class Author(models.Model):
name = models.CharField(max_length = 128)
document = models.ManyToManyField(Document, through = 'Foo')
def __unicode__(self):
return self.name
class Foo(models.Model):
document = models.ForeignKey(Document)
author = models.ForeignKey(Author)
author_order = models.IntegerField()
class FooInline(admin.TabularInline):
model = Foo
class DocumentAdmin(admin.ModelAdmin):
inlines = [ FooInline ]
admin.site.register(Author)
admin.site.register(Document, DocumentAdmin)
http://docs.djangoproject.com/en/dev/topics/db/models/#intermediary-manytomany
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#inlinemodeladmin-objects
this might help you too - there is some utility in Django to build models for the legacy databases.
I've built a model for mediawiki database using it. It gets most of the things right, but you'll need to tweak the models a little.