How can i extend django sites framework?
I need to add more fields, like logo file, subtitle, etc.
Or any other solution for this kind of thing?
just make OneToOne Relation with Site Model
from django.contrib.sites.models import Site
class CustomSite(models.Model):
class Meta:
verbose_name = "Custom Domain"
verbose_name_plural = "Custom Domains"
site = models.OneToOneField(Site, null=True, related_name='customsite')
subtitle = models.CharField(max_length=100)
#...
#...
def __unicode__(self):
return 'Customsite of {0}'.format(self.site.domain)
Related
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 :)
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 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.
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()]
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.