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
Related
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
Im new in django , When im learning django from documentation .i tried to make api using serializers and Function Based views .the this shows type object 'Questions' has no attribute 'objects'
models.py
class Questions:
title = models.CharField(max_length=40)
description = models.TextField(max_length=50)
status = models.CharField(default='inactive', max_length=30)
created_by = models.ForeignKey(User, null=True, blank=True, on_delete=models.CASCADE)
serializers.py
from rest_framework import serializers
from demoapp.models import Questions
class QuestionSerializer(serializers.ModelSerializer):
class Meta:
model = Questions
fields = (
'id',
'title',
'description',
'created_by',
)
urls.py
from django.urls import path
from demoapp.views import *
urlpatterns = [
path('poll', demoapp),
]
And Error is:
enter image description here
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 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!
i just defined new class for my project. When I run server i dont recive errors just dont appears on the admin-django Panel.
Here is the new class from models.py
class Stock_Clinicas(models.Model):
organization = models.ForeignKey(Organization, null=True, blank=True, on_delete=models.SET_NULL)
products = models.ForeignKey(Products, null=True, blank=True, on_delete=models.SET_NULL)
provider = models.ForeignKey(Provider, null=True, blank=True, on_delete=models.SET_NULL)
stock_min = models.DecimalField('Stock minimo', max_digits=10, decimal_places=0)
stock_opt = models.DecimalField('Stock optimo', max_digits=10, decimal_places=0)
stock_act = models.DecimalField('Stock actual', max_digits=10, decimal_places=0, default=0)
and here my admin.py. i'll put all code.
from django.contrib import admin
from stock.models import *
from .models import Organization, OrganizationUser, OrganizationOwner
class OwnerInline(admin.StackedInline):
model = OrganizationOwner
raw_id_fields = ('organization_user',)
class OrganizationAdmin(admin.ModelAdmin):
inlines = [OwnerInline]
list_display = ['name', 'is_active']
prepopulated_fields = {"slug": ("name",)}
class OrganizationUserAdmin(admin.ModelAdmin):
list_display = ['user', 'organization', 'is_admin']
raw_id_fields = ('user', 'organization')
class OrganizationOwnerAdmin(admin.ModelAdmin):
raw_id_fields = ('organization_user', 'organization')
class Stock_ClinicasAdmin(admin.ModelAdmin):
list_display = ('stock_min', 'stock_opt', 'stock_act')
admin.site.register(Stock_Clinicas, Stock_ClinicasAdmin)
admin.site.register(Organization, OrganizationAdmin)
admin.site.register(OrganizationUser, OrganizationUserAdmin)
admin.site.register(OrganizationOwner, OrganizationOwnerAdmin)
Forget said, when I use syncdb the tables aren't created. I'm using Django 1.5.
dont resolved yet
You have a few issues:
class Stock_ClinicasAdmin(admin.ModelAdmin):
list_display = ('Stock Minimo', 'Stock Actual', 'Stock Optimo')
You don't have any fields called Stock Minimo, the field is stock_min, so you need to adjust this.
If you just created this app, make sure:
You add it to INSTALLED_APPS; it should have stock,
You might want to run manage.py makemigrations and manage.py migrate to create the tables.
In admin.py, rename your modelAdmin class to Stock_ClinicasAdmin (or anything else that your model name), and add this after the modelAdmin class declaration:
admin.register(Stoc_Clinicas, Stock_ClinicasAdmin)