I don't have the advertisement module displayed in the django admin panel. Here is the model code
from django.db import models
class Advertisement(models.Model):
title = models.CharField(max_length=1000, db_index=True)
description = models.CharField(max_length=1000, default='', verbose_name='description')
creates_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
price = models.FloatField(default=0, verbose_name="price")
views_count = models.IntegerField(default=1, verbose_name="views count")
status = models.ForeignKey('AdvertisementStatus', default=None, null=True, on_delete=models.CASCADE,
related_name='advertisements')
def __str__(self):
return self.title
class Meta:
db_table = 'advertisements'
ordering = ['title']
class AdvertisementStatus(models.Model):
name = models.CharField(max_length=100)
admin.py /
from django.contrib import admin
from .models import Advertisement
admin.site.register(Advertisement)
I was just taking a free course from YouTube. This was not the case in my other projects. Here I registered the application got the name in INSTALLED_APPS. Then I performed the creation of migrations and the migrations themselves. Then I tried to use the solution to the problem here , nothing helped. I didn't find a solution in Google search either.
127.0.0.1:8000/admin/
console
admins.py
The name of the file is admin.py not admins.py. Yes, that is a bit confusing since most module names in Django are plural. The rationale is probably that you define a (single) admin for the models defined.
Alternatively, you can probably force Django to import this with the AppConfig:
# app_name/apps.py
from django.apps import AppConfig
class AppConfig(AppConfig):
def ready(self):
# if admin definitions are not defined in admin.py
import app_name.admins # noqa
Related
I have created a new model for my app Consumptions but it doesn't show up.
I know that I have to put it on the admin.py page but still not working.
I don't know what could be happening
This is my models.py page:
from logs.mixins import LogsMixin
# Other models
class MF(LogsMixin, models.Model):
"""Definición del modelo de Proveedor."""
name = models.CharField("Nombre", null=False, default="MF", max_length=50)
class Meta:
verbose_name = 'Módulo formativo'
verbose_name_plural = 'Módulos formativos'
def __str__(self):
return self.name
# Other models
And this is my admin.py page:
from django.contrib import admin
from .models import Provider, Consumption, Message, Course, Call, Platform, MF
admin.site.register(Provider)
admin.site.register(Consumption)
admin.site.register(Message)
admin.site.register(Course)
admin.site.register(Call)
admin.site.register(Platform)
admin.site.register(MF)
As you can see is not my only model, I do the same with all of them but the MF one is not showing up on the admin page.
What am I doing wrong?
Try following approach:
class ProviderAdmin(admin.ModelAdmin):
model = Provider
admin.site.register(Provider, ProviderAdmin)
In ProviderAdmin class you may specify filters, search fields or empty value handling:
search_fields = ('',)
list_filter = ('',)
empty_value_display = 'empty'
I'm trying to hide and delete two fields from showing in a form I created in the Django administration page using ModelForm.
I looked at answers that said I should use the "exclude" meta field, but I don't know why it's not working in my case.
Here is my code:
models.py:
class Activity(models.Model):
type = models.CharField(max_length=50, default="")
title = models.CharField(max_length=200, default="")
description = models.CharField(max_length=500)
owner = models.ForeignKey(User, related_name="owner")
college = models.CharField(max_length=200)
location = models.CharField(max_length=200)
room = models.CharField(max_length=200)
startDate = models.DateTimeField(null=True, blank=True)
endDate = models.DateTimeField(null=True, blank=True)
attendee = models.ManyToManyField(Attendee, related_name="attendees",null=True, blank=True)
volunteer = models.ManyToManyField(Volunteer, related_name="volunteers",null=True, blank=True)
I'm trying to exclude the "attendee & volunteer" fields from displaying in the Django administration form.
In admin.py I have:
from django.contrib import admin
from django import forms
from KSUvity.models import Activity
class ActivityForm(forms.ModelForm):
class Meta:
model = Activity
exclude = ['attendee', 'volunteer',]
class ActivityAdmin(admin.ModelAdmin):
exclude = ['attendee', 'volunteer',]
form = ActivityForm
admin.site.register(Activity, ActivityAdmin)
You have to create an admin.py file in your app and register your models
Follow the instuctions
See the example below
from django import forms
from django.contrib import admin
from myapp.models import Person
class PersonForm(forms.ModelForm):
class Meta:
model = Person
exclude = ['name']
class PersonAdmin(admin.ModelAdmin):
exclude = ['age']
form = PersonForm
admin.site.register(Person, PersonAdmin)
You can use either fields or exclude in one class.
In your app admin field add this code.
app_name/admin.py
from django.contrib import admin
class ActivityAdmin(admin.ModelAdmin):
exclude = ('attendee', 'volunteer',)
You have to use ModelAdmin option to exclude fields from form in Django administration, either ModelAdmin.exclude or ModelAdmin.fields. Below is an example:
class ActivityAdmin(admin.ModelAdmin):
exclude = ('attendee', 'volunteer', )
To make it work, you register model like this:
admin.site.register(Activity, ActivityAdmin)
You add this code to admin.py file.
I have these models below
# user profile models file
from ad.models import FavoriteAd
class UserProfile(models.Model):
def get_user_favorite_ad(self):
return FavoriteAd.objects.filter(fav_user=self)
# ad models file
from user_profile.models import UserProfile
class FavoriteAd(models.Model):
fav_user = models.ForeignKey(UserProfile, blank=False, on_delete=models.CASCADE)
I have tried using these but it give me the NameError UserProfile not found
# ad models files
class FavoriteAd(models.Model):
fav_user = models.ForeignKey('user_profile.UserProfile', blank=False, on_delete=models.CASCADE)
Also tried these as well still got error that model are not ready
# ad models files
from django.apps import apps
UserProfile = apps.get_model('user_profile', 'UserProfile')
class FavoriteAd(models.Model):
fav_user = models.ForeignKey(UserProfile, blank=False, on_delete=models.CASCADE)
You are using FavoriteAd inside get_user_favorite_ad method of
UserProfile model
Thats the reason you are unable to import it in FavoriteAd and this is causing circular import.
For fetching favorite ads of that user, Use favoritead_set to get related objects
# remove that import as well
# from ad.models import FavoriteAd
class UserProfile(models.Model):
def get_user_favorite_ad(self):
return self.favoritead_set.all()
I am working on a Wagtail CMS project. Everything has been working fine so far. But when I started translating each model field using Wagtail-modeltranslation package (wagtail-modeltranslation==0.6.0rc1), the model with ParentalKey is not showing properly in my admin interface. I am including my model and translation.py files along with this. It would be greate if someone gives a helping hand to find a solution to this problem.
class HomePage(Page):
# Home page fields
home_page_title = models.CharField(max_length=200)
short_description = RichTextField(null=True, blank=True)
main_image = models.ForeignKey('wagtailimages.Image', null=True,
blank=True, on_delete=models.SET_NULL,
related_name='+')
content_panels = Page.content_panels + [
FieldPanel('home_page_title'),
...
...
...
InlinePanel('related_partners', label="Related Parnters"),
#Child class
class Partner(Orderable):
partners = ParentalKey(TranslationChild,
related_name='related_partners', null=True,
blank=True)
partner_image = models.ForeignKey('wagtailimages.Image',
null=True, blank=True, on_delete=models.SET_NULL,
related_name='+')
partner_url = models.URLField(max_length=500)
And my translation.py file looks like this,
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from .models import HomePage, TranslationChild, Partner
from wagtail_modeltranslation.translation import TranslationOptions
from modeltranslation.decorators import register
#register(HomePage)
class FooTR(TranslationOptions):
fields = (
'home_page_title',
'short_description',
'body',
'translation_section_title',
)
#register(Partner)
class PartnerTR(TranslationOptions):
fields = (
'partner_description',
'partners',
)
Here in the admin page, the partner section is not showing properly. Django version 1.8.5 and Wagtail version 1.9
I have problem about the abstract model in chapter "Adding Easy Admin Emails, Helpers, Sitemaps, and More" (the hello web app intermediate book)
here is my models.py code:
from django.contrib.auth.models import User
from django.db import models
class Timestamp(models.Model):
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
class Thing(Timestamp):
name = models.CharField(max_length=225)
class Thing(models.Model):
name = models.CharField(max_length=225)
description = models.TextField()
slug = models.SlugField(unique=True)
user = models.OneToOneField(User, blank=True, null=True)
def get_absolute_url(self):
return "/things/%s/" % self.slug
class Social(models.Model):
SOCIAL_TYPES = (
('twitter','Twitter'),
('facebook','Facebook'),
('pinterest','Pinterest'),
('instagram','Instagram'),
)
network = models.CharField(max_length=255, choices=SOCIAL_TYPES)
username = models.CharField(max_length=255)
thing = models.ForeignKey(Thing,related_name="social_accounts")
class Meta:
verbose_name_plural = "Social media links"
when I run
$python manage.py makemigrations
$python manage.py migrate
the error message shows:
/helloApp/venv/lib/python2.7/site-packages/django/db/models/base.py:309: RuntimeWarning: Model 'collection.thing' was already registered. Reloading models is not advised as it can lead to inconsistencies, most notably with related models.
new_class._meta.apps.register_model(new_class._meta.app_label, new_class)
No changes detected
that does not show like in the book :
You are trying to add a non-nullable field 'added' to thing without a defaul,,,,,,,
any answer would very greatful thank you!