Using Wagatil Admin widgets on Public Front end - django

I have Streamfields and some custom StructBlocks, StreamBlocks and RichText on wagatil Pages & Models. Which are very easy to edit when I'm logged in as an Admin.
However I would like my end users to create and Edit these fields using the widgest available to me in the admin. How do i expose them to the public without giving them the ability to login to my admin?
I cam across this article that looked promising but ultimatley I got an error and couldn't test.
from wagtail.admin.forms.models import WagtailAdminModelForm
class FeaturedImage(models.Model):
date = models.DateField()
class FeaturedImageForm(WagtailAdminModelForm):
class Meta:
model = FeaturedImage
fields = ['date']
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
What i want to know is am i working in the correct direction? If so how do I resolve the above error?

Related

GeoDjango Admin displaying openlayers map instead of open street map in Admin

i am have enabled everything needed to work with spatial data at the database and django setting level, my profile model has a default_location field that is a PointField. as shown below.
from django.contrib.gis import models
class Profile(models.Model):
...
default_location = models.PointField()
i registered the profile model as an inline to be viewed and edited from within a User model (one-to-one relationship between user and profile).code shown below
class ProfileInline(StackedInline):
model = models.Profile
class NewUserAdmin(admin.GISModelAdmin):
gis_widget = OSMWidget
inlines = [ProfileInline]
admin.site.unregister(models.User)
admin.site.register(models.User, NewUserAdmin)
however i keep getting a openlayer map in my django admin page
please can anyone suggest a fix to this. i need open street map because of it detailed street feature.
You can use the django-leaflet package. By default an OpenStreetMap is displayed, and it also has better tools and interface.
After installing you need add leaflet to INSTALLED_APPS in settings.py.
Then you use LeafletGeoAdmin in your ModelAdmin in admin.py.
You can add some customizations by adding this to your settings.py:
LEAFLET_CONFIG = {
'DEFAULT_CENTER': (39.694819, -8.130229),
'DEFAULT_ZOOM': 6,
'MAX_ZOOM': 20,
'MIN_ZOOM':3,
'SCALE': 'both'
}
More information here: https://django-leaflet.readthedocs.io/

Differences between Stacked inline and Tabular inline

This is my models.py file
from django.db import models
# Create your models here.
class Item(models.Model):
name=models.CharField(max_length=250)
description = model.TextField()
class Meta:
oredering['name']
def __unicode__(self):
return self.name
#permalink
def get_absolute_url:
retun ('item_detail',None,{'object_id':self_id})
class Photo(models.Model):
item = models.ForiegnKey(Item)
title=models.ChaField(max_length=250)
image=models.IMageField(upload_to='photos')
caption=models.TextField(blank=True)
class Meta:
ordering=['title']
def __unicode__(self):
return self.title
#permalink
def get_absolute_url(self):
retun ('photo_detail',None,{'object_id':self_id})
And this is my admin.py :
from django.contrib import admin
from models import Item
from models import Photo
# Register your models here.
class PhotoInline(admin.StackedInline):
model = Photo
class ItemAdmin(admin.ModelAdmin):
inlines = [PhotoInline]
admin.site.register(Item, ItemAdmin)
admin.site.register(Photo)
But, I can't understand what is StackedInline and TabularInline, I referred to Django documentation but still couldn't understand what exactly it was.
Also, I can't see those models in my admin panel when I started the server, I don't understand why my models aren't registered on my admin page.
I see two different questions:
I cant understand what is stacked inline and tabular inline
Basically, both allow you to edit models on the same page as a parent model. In other words, it is sometimes preferable for a user to have the possibility to edit a certain model while editing another one instead of having to manually add another instance somewhere else in your interface. In your particular case, you can use it to facilitate user experience by allowing the user to add photos linked to a parent item simultaneously whitout having to constantly change between your admin forms.
Now, the difference between the two is really simple to understand: Layout. Indeed, both works exactly the same behind the scenes, the only difference is the template used for rendering. It can be seen here in the source code. So, picking one for your project is only a matter of preference regarding the interface layout
I cant see those models in my admin panel
This can be a lot of things, but often it's because you forgot to run your migrations with makemigrations and migrate. If you did, another thing that a lot of users forget is to install their app. So, in
Setting.py
INSTALLED_APPS = ['Myproject.apps.Myapp']
The TabularInline displays data in table
But StackedInline displays in row

In Django, is there a way to show the admin for a model under a different app?

I've extended the standard User with a Profile model using a one-to-one relationship:
class Profile(models.Model):
user = models.OneToOneField(User, primary_key=True, editable=False)
# Additional user information fields
This is in an app called account_custom. The issue is that in the admin site, I would like this model to show up along with User under "Authentication and Authorization".
I was able to make this show up in the right place in the admin by setting Meta.app_label:
class Profile(models.Model):
class Meta:
app_label = 'auth'
db_table = 'account_custom_profile'
I set Meta.db_table so this uses the existing database table, and everything seems to function properly.
The problem is that Django wants to generate migrations for this, one in account_custom to delete the table and one in auth to create it again. I'm guessing this would either fail or wipe out the data depending on the order the migrations are run, but the deeper problem is that it's trying to create a migration in auth which isn't part of my code base.
Is there a way around this, or is there some better way to control where a ModelAdmin shows up in the admin site?

Django: 'no such table' after extending the User model using OneToOneField

(Django 1.10.) I'm trying to follow this advice on extending the user model using OneToOneField. In my app 'polls' (yes, I'm extending the app made in the 'official' tutorial) I want to store two additional pieces of information about each user, namely, a string of characters and a number.
In my models.py I now have the following:
from django.contrib.auth.models import User
class Employee(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
stopien = models.CharField(max_length=100)
pensum = models.IntegerField()
and in admin.py the following:
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User
from polls.models import Employee
class EmployeeInline(admin.StackedInline):
model = Employee
can_delete = False
verbose_name_plural = 'employee'
class UserAdmin(BaseUserAdmin):
inlines = (EmployeeInline, )
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
When adding a user using the admin panel my two new fields display correctly. However, when I click 'save', or if I don't add any user and just click on the name of my sole admin user in the admin panel, I get the following error:
OperationalError at /admin/auth/user/1/change/
no such table: polls_employee
I see some questions and answers related to similar problems, but they seem to be relevant for older version of Django. Could anyone give me a tip as to what I should do? Ideally I'd want my two additional fields display in the admin panel, though I suspect this might be a task for the future.
I have to confess I do not understand this paragraph from the documentation just following the advice I'm using:
These profile models are not special in any way - they are just Django models that happen to have a one-to-one link with a User model. As such, they do not get auto created when a user is created, but a django.db.models.signals.post_save could be used to create or update related models as appropriate.
Do I need to tie this 'post-save' to some element of the admin panel?
I'd be very greatful for any help!
You need run makemigrations to create a migration for your new model, and then migrate to run the migration and create the database table.
./manage.py makemigrations
./manage.py migrate

Django admin order by most recent related model creation

I have some models for a messaging system on my django site.
class Enquiry(models.Model):
etc...
class Message(models.Model):
enquiry = models.ForeignKeyField(Enquiry)
sent_at = models.DateTimeField(default=timezone.now)
etc...
Each message has a foreign key to an enquiry. In the admin site I would like to be able to order the enquiries by the most recent received message. In a regular view, I can do this:
Enquiry.objects.annotate(latest_message=Max('message__sent_at')).order_by('-latest_message')
Is there a way to achieve this in the admin framework?
You can override the get_queryset method for your model admin, and use the same approach that already works in your views.
My first suggestion was to try setting ordering for your model admin, but you reported that it didn't work.
class EnquiryAdmin"
ordering = ['-message__sent_at']