Mezzanine custom field adding to admin - python

I am writing a piece of software and have added an extra field using mezzanines model injection ability's the field is there and can be seen in the database but following the method shown in the docs on this page mezzanine model customisation I cannot get it to show up in my admin page bellow is the code I have written that I believe should work to de-register and then re-register with the new field added to the admin. I am unsure if this code is even running it is in the root of my project as I believe that is where it should be and named admin.py as I believe it should be and have used that file in other projects.
from copy import deepcopy
from django.contrib import admin
from catridge.shop.admin import ProductAdmin
from catridge.shop.models import Product
product_fieldsets = deepcopy(ProductAdmin.fieldsets)
product_fieldsets[0][1]["fields"].insert(-2, "download_file")
class MyProductAdmin(ProductAdmin):
fieldsets = product_fieldsets
admin.site.unregister(Product)
admin.site.register(Product, MyProductAdmin)
Does anyone have any idea as to how this might work I am a bit stumped currently but this is mainly a mezzanine function.

The admin.py file needs to go inside one of your INSTALLED_APPS directories.

Related

Django project without models and data base

It is possible to build a project in Django without models ? I have a views, templates(html), css and urls. That site is looking very good in a browser. It is a hairdressing salon website.
Greetings
It is completely possible to create a Django project without any models. You only really need models if your website contains objects, like posts or users.
For one example, I created a recreation of Wikipedia using Django using exactly 0 models; since each entry was just a Markdown file, there was no need for any models.
Sure thing! Just keep in mind that Django's built in admin suite needs a superuser to access, so if you want to manage your website from that side of things, you will need to put in at least a User model, but thankfully that is pretty easy.
models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
pass
If this is your models.py file, you can just run python manage.py createsuperuser

How to add page templates to Wagtail after installing into an existing Django application

I'm trying to extend an already existing Django App. The app is functioning fine as is, but I would like to add blog functionality.
I've installed Wagtail, using the guidelines here (http://docs.wagtail.io/en/latest/getting_started/integrating_into_django.html) To check wagtail is installed, I have navigated to here:
http://myurl/cms
And the wagtail admin panel is displayed. When I navigate to http://myurl/admin I get the default admin control panel for my Django app, so far so good.
Now I am trying to build the blog.
I found this tutorial:
http://wiseodd.github.io/techblog/2015/06/22/developing-wagtail/
which suggests the following as a first step: -
First, we’ll create our generic page class as home page class is
already created by default when we started Wagtail project.
It then displays this code:
# core/models.py
from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailadmin.edit_handlers import FieldPanel
from wagtail.wagtailsearch import index
# We’re deriving our GenericPage from Page class, so that our GenericPage also has Page’s field, e.g. title
class GenericPage(Page):
# Let’s create our custom field, named body which is a rich text
body = RichTextField()
# Index the body field, so that it will be searchable
search_fields = Page.search_fields + (index.SearchField(‘body'),) # To show our body field in admin panel, we have to wrap it with FieldPanel and add it to Page’s field panel content_panels = Page.content_panels + [FieldPanel('body', classname=‘full’)]
I could not find which file I was meant to add this into. I searched the system using grep, and found a number of files that had the text string:
from wagtail.wagtailcore.models import Page
I decided the most likely candidate was in the directory:
env/lib/python2.7/site-packages/wagtail/project_template
Within my original app directory. I added the code above to the models.py file residing in the above directory. I then ran
python manage.py makemigrations
But it said no migrations were found. The next step in the tutorial posted above suggests you should now see three different page types available to create in the control panel, but I can not find the option to create any pages.
Can you tell me if I edited the correct file above, or if I should have edited a different file, and also
Why I am not seeing any option to add a new page in the wagtail control panel?
I have consulted with the documentation here (http://docs.wagtail.io/en/latest/getting_started/tutorial.html) and tried following the 'extend the homepage model' section, but couldn't figure out where the home/models.py file is as there is no folder called home in my Django app.
Thanks for any advice
As the final section of the "integrating into Django" docs says:
You’re now ready to add a new app to your Django project (via ./manage.py startapp - remember to add it to INSTALLED_APPS) and set up page models
Running ./manage.py startapp blog will add a blog app to your project, including an empty models.py - this is where you add your page definitions. (The Wagtail docs don't go into detail on this, because it's just following the standard Django workflow, which is hopefully familiar to anyone with an existing Django project to integrate with...)
Tutorials that use wagtail start my_project as a starting point will omit this step, because the starter project comes with a pre-made models.py with a HomePage model. The site-packages/wagtail/project_template directory you found is actually the 'master' copy of the starter project, which gets cloned at the point that you run wagtail start my_project. Since this isn't hooked up to your current project, changing it had no effect.

In the Django admin, "Add" and "Change" options are not appearing for a brand new simple model admin. Why?

This is probably something simple, but I'm just not seeing it. So I started a new app called proposals. The following I entered into the app's models.py:
from django.db import models
class Proposal(models.Model):
name = models.CharField(max_length=200)
I then entered the following into admin.py:
from django.contrib import admin
from proposal.models import Proposal
class ProposalAdmin(admin.ModelAdmin):
pass
admin.site.register(Proposal, ProposalAdmin)
This seems incredibly straightforward, right out of the Django docs, but the "Add" and "Change" options for this admin are not appearing. They do appear for everything else, but not this particular admin. Any ideas on what might be causing this?
UPDATE
I'm continuing to check into this, and it's not a permission issue. The permissions are all set to true for add / change / delete. The template that generates the admin page requires a model.add_url for an "Add" link and model.admin_url for a "Change" link. These attributes are missing for some reason. Thus no links. Why they are missing is the big question.
UPDATE
I finally discovered what was going on here. An app which didn't need the add / change links was listed in the installed apps prior to this new one. Whatever it's author did to cause these links not to appear ended up being inherited by all other apps that followed it in this list. When I moved this new app above this one, the links appeared. I didn't even know that it was possible for apps to affect each other like this, but that is something to look for in the future.

How to add HTML content editor in Django admin view?

How would I add a rich-text HTML content editor in my Django admin view?
For example, if I want to change the content on my homepage, what python code would I have to input for the HTML to be displayed when I log into the admin portal?
I want to be able to view all of my pages (hopefully even be able to add/delete them), and edit the content directly from the admin view. Similar to something like this:
http://feincms-django-cms.readthedocs.org/en/latest/_images/item_editor_content.png
I appreciate any and all help! Thank you!
The html shown there is there cuz it is in the database. If what you want is a cms, there are several of those for django like django-cms, wagtail, mezzanine among others.
If, on the other hand, you have a model already and want to display it in the admin, you can register it:
from django.contrib import admin
from myproject.myapp.models import Author
class AuthorAdmin(admin.ModelAdmin):
pass
admin.site.register(Author, AuthorAdmin)
In that example, the Author model will show up in admin for edition.
In the last case, where you already have your model registered but you want to have a WYSIWYG editor, check the django-grid for that kind of package and find one suitable, or install one manually.
You have several options in case you want to try the second approach: ckeditor, tinymce, etc.

How to create a personalized Django admin model?

I am writing a Django application and I need to create a personalized admin model, in order to show only determined items in the administration interface when you create a new object.
I wrote this code for admin.py:
from django import forms
from myapp.models import *
from django.contrib import admin
class SimAdmin(admin.ModelAdmin):
phone = forms.ModelChoiceField(queryset=Item.objects.filter(name='phone'))
fields = ('phone', 'num_phone', 'pin', 'puk')
admin.site.register(Item)
admin.site.register(Sim, SimAdmin)
...
The problem is that when I log in to the admin site, my SimAdmin does not appear. Django does not give me any error in this code, but if I write only
...
admin.site.register(SimAdmin)
...
it gives me a
TypeError: 'MediaDefiningClass' object
is not iterable
in this line.
I searched in the web and the documentation for this error, but I didn't found anything relevant about my concrete problem. I think the solution will be very simple, but I can't see it. Can anybody help me?
Thanks!
this is incorrect:
class SimAdmin(admin.ModelAdmin):
phone = forms.ModelChoiceField(queryset=Item.objects.filter(name='phone'))
you're trying to attach forms.Field to admin.ModelAdmin. It doesn't work this way.
Take a look at how can it be implemented: http://docs.djangoproject.com/en/1.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_manytomany

Categories

Resources