Basically I am trying to recreate a fields in this model that is similar to django user authenticate system, for this I try using many-to-many fields but it ended up like this.
I am trying to have another field that can show what exist and another field for I have chosen similar to django admin, which looks like this.
This is my code
class Category(models.Model):
name = models.CharField(max_length=128, unique=True)
class BlogPage(models.Model):
category = models.ManyToManyField(Category)
title = models.CharField(max_length=128)
preview = models.TextField(max_length=256)
content = models.TextField()
I believe what you want is a filter_horizontal widget used in the Admin template. This should help: Django Admin ManyToManyField
currently i am able to display many to many fields is admin panel
models.py
class Abc(models.Model):
course = models.ManyToManyField(Course, blank=True)
admin.py
class AbcsAdmin(admin.ModelAdmin):
"""
Admin panel management for Alumni
"""
list_display = ["id","get_course"]
def get_course(self,obj):
return [course.name for course in obj.course.all()]
Related
I have these 2 models in my models.py
class Student(AbstractBaseUser):
date_of_birth = models.DateField(null=True)
phone_number = PhoneNumberField(null=True)
class Teacher(Student):
bio = models.TextField(null=True)
image = CompressedImageField(null=True, blank=True, default="default.png")
student = models.ForeignKey(Student, on_delete=models.SET_NULL, null=True)
Now in admin panel when i go to edit the Teacher instance I display only bio image and student. The issue is that also display those fields when I try to edit Teacher instance but entering from Student model. So is there a way for that?
P.S the models and fields may not make sense because they are examples.
Use ModelAdmin.fields or ModelAdmin.exclude to control which fields you want to include or exclude in the admin site form.
from django.contrib import admin
class TeacherAdmin(admin.ModelAdmin):
exclude = ("date_of_birth", "phone_number")
And register it like so
admin.site.register(Teacher, TeacherAdmin)
i have created a model Post
class Post(models.Model):
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=100)
image = models.ImageField(upload_to='blog_image', default='default.jpg')
smallContent = models.TextField()
content = models.TextField()
data_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.title
but id field is not showing in admin panel. i did makemigrations and migrate and both are done successfully.
AutoField is generated after the model is saved. It is not provided in Admin Panel as it should not be editable.
If want to make id editable (not recommended) override it as a CharField instead of AutoField with primary_key=True.
Else if just want to show it in the panel (while editing a saved model), add it to the read_only list of your model admin.
Read about AutoField here.
Since it is a readonly field I think you probably need to explicitly tell admin to show it.
I don't know if this will work but try in admin.py
from django.contrib import admin
from .models import Post
class PostAdmin(admin.ModelAdmin):
readonly_fields = ['display_id']
def display_id(self, obj):
return obj.id
admin.site.register(Post, PostAdmin)
How to have multiple tables(queryset) on one page in django admin.
For example:
When i go to the company page, i can see the list of departments in the company, i can also see the list of employees in the company.
You can use InlineModelAdmin objects to implement this, although if you want to do nested inlines you should check out this post. Although as that post says:
...it would be a kind of convoluted design to implement.
You didn't provide any code here so the best I can do is guess your model relationships.
models.py
from django.db import models
class Department(models.Model):
name = models.CharField(max_length=250)
...
class Employee(models.Model):
name = models.CharField(max_length=250)
...
class Company(models.Model):
name = models.CharField(max_length=250)
departments = models.ForeignKey(Department)
employees = models.ForeignKey(Employee)
...
admin.py
from django.contrib import admin
class EmployeeInline(admin.StackedInline):
model = Employee
class DepartmentInline(admin.StackedInline):
model = Department
class CompanyAdmin(admin.ModelAdmin):
list_display = ('name')
inlines = [DepartmentInline, EmployeeInline]
admin.site.register(CompanyAdmin)
I need to add job offers section to my company site (as a intro to django for me). The problem is that when i inherit my model from mezzanine's Page model it adds to admins create form all bunch of field which i dont need (like publish dates, draft field, comment field etc). I want to make create/edit job offers form as simple as possible.
I tried to inherit it from basic models.Model but it throws an error ...
Unknown column 'project_joboffer.id' in 'field list'"
I tried to customize Admin Form but im still getting error above.
models.py
class JobOffer(models.Model):
title = models.CharField(max_length=255, null=False, blank=False)
place = models.CharField(max_length=255, null=True, blank=True)
date = models.DateTimeField(auto_now_add=True)
content = models.TextField(blank=False,null=False)
published = models.BooleanField(default=True)
deleted = models.NullBooleanField()
forms.py
from django import forms
from ckeditor.widgets import CKEditorWidget
from models import JobOffer
class JobOfferForm(forms.ModelForm):
title = forms.CharField(max_length=255, required=True)
place = forms.CharField(max_length=255, required=False)
content = forms.CharField(required=True , widget=CKEditorWidget())
published = forms.BooleanField(initial=True)
deleted = forms.NullBooleanField()
# class Meta:
# model = JobOffer
admin.py
class JobOfferAdmin(admin.ModelAdmin):
form = JobOfferForm
admin.site.register(JobOffer, JobOfferAdmin)
OK, i fixed it. Migrations creating and deleting wasnt enough. I dont know why but this time i had to also delete entry in django_migrations table.
I have 2 models.
One has a foreign key to another.
Say:
class Organization(models.Model):
title = models.CharField(max_length=300)
class User(models.Model):
name = models.CharField(max_length=300)
organization = models.ForeignKey(Organization)
I want to show all users who work in current organization in organizationAdmin, like a list of change links.
It should be possible
to add new user
to select existing user(no related to current organization) and set his organization to current
No need to edit them inline
Just like permissions in userAdmin + adding and changing them
Is there any ready solution? Not to make forms by hands
You can do that with exclude:
class UserInline(admin.TabularInline):
model = User
exclude = ['name', 'other_fields']
Or you can use ManyToMany relation:
class User(models.Model):
name = models.CharField(max_length=300)
class Organization(model.Model):
title = models.CharField(max_length=300)
users = models.ManyToManyField(User)