django framework can't see new model online - python

I have to make some modification to a website built with the django framework (version 1.6).
The problem I am having is that I can not transfer the modification I made offline to the online server. I can see the new page, the database has been modified, but in the administration website the new category I created does not appear, thus i can not fill the fields with the new information.
The steps I went through are:
create the new model in models.py,
import the model in admin.py,
execute the command: touch wsgi.py
in order to reload the file.
In the offline version everything is working fine. I do not know that to do!
Code I added in admin.py file (I added the "Article" section):
from active.models import x, z, y, j, Article
class ArticleAdmin(admin.ModelAdmin):
ordering = ["title"]
list_display = ('title',)
search_fields = ['title']
admin.site.register(Article, ArticleAdmin)

Solved.
There was no problem at all, simply the user I access the administration site with did not have the right access to see the new section!

Have you imported admin in the admin.py file?
from django.contrib import admin

Related

Access Django DB objects without shell?

I have a request - can you help me access and manage django DB objects without using shell ?
I have created my models, but now (for example) i want to make a login system. I store users and passes(again, only an example), and i want to get the info from the DB, but i dont want to use shell.
What can i do in this case, im quite new to Django ?!
Best Regards
Why not use django-admin?
Maybe this is what you want:https://docs.djangoproject.com/en/3.0/ref/contrib/admin/
In views.py you can import
from .models import modelname
data = modelname.objects.all() - using this you can get all the data from the Database
Eg:-
for d in data:
print (d.email)
Will give all emails in the database
You can also use
t = modelname.objects.get(email='name#lk.com')
By this you can get the data of the person who's email is name#lk.com
Django already has database support where you can register your models and access them with a graphical interface.
See the documentation: django-admin-site
First you need to create a super user account, if you don't have one, create it with the terminal in the project directory, use this row:
python manage.py createsuperuser
For your model to appear on the admin site you need to register it
# models.py
class MyModel(models.Model)
field1 = models.CharField()
field2 = models.TextField()
# ...
# admin.py
from django.contrib import admin
from .models import MyModel
admin.site.register(MyModel)
So it's the basic way to register your model, if you want to personalize you need to check the documentation ModelAdmin.fieldsets
with this done, just access the admin site at the link http://localhost:8000/admin/ and log in with the super user account and you will see the model registered.

Manager isn't available while setting up django-fmc

I'm trying to get django-fmc set up with Django (v 1.97, Python v2.7.12, djangorestframework v3.3.3) to handle storing registration ids and sending notifications to devices. I am following the tutorial they provide but it doesn't seem to be working.
I am getting the following error when running my local server and python manage.py fcm_urls:
...
File "C:\Work\Dev\LiveTracking\Api\app\views.py", line 50, in DeviceViewSet
queryset = Device.objects.all()
File "C:\Work\Dev\LiveTracking\Api\env\lib\site-packages\django\db\models\manager.py", line 277, in __get__
self.model._meta.swapped,
AttributeError: Manager isn't available; 'fcm.Device' has been swapped for 'app.MyDevice'
I don't want to add additional fields to the MyDevice model for now. I've looked all over but can't fix this error. If anyone can shed some insight into this error it would be much appreciated.
Here are some of my code snippets:
settings.py
INSTALLED_APPS = (
'fcm',
)
# Firebase Cloud Messaging Key
FCM_APIKEY = 'AIzaSyCaqHZIcaGDOpfTZUmAHEowsqD-fCtow6A'
# Location of device model
FCM_DEVICE_MODEL = 'app.MyDevice'
serializers.py
from fcm.models import Device
class DeviceSerializer(serializers.ModelSerializer):
class Meta:
model = Device
fields = ('dev_id','reg_id','name','is_active')
views.py
from rest_framework import viewsets
from fcm.models import Device
from fcm.serializers import DeviceSerializer
class DeviceViewSet(viewsets.ModelViewSet):
queryset = Device.objects.all()
serializer_class = DeviceSerializer
urls.py
from rest_framework import routers
from fcm.views import DeviceViewSet
router = routers.DefaultRouter()
router.register(r'devices', DeviceViewSet)
urlpatterns = [
url(r'^v1/', include(router.urls)),
]
swappable is an undocumented feature, actually only supposed to be used for custom User models. The doc on custom user models clearly states that once you use a custom user model, directly referencing contrib.auth.models.User won't work:
If you reference User directly (for example, by referring to it in a foreign key), your code will not work in projects where the AUTH_USER_MODEL setting has been changed to a different user model.
You probably want to read the rest of this chapter FWIW.
To make a long story short: as Daniel Roseman mentions, you very probably want to use your own MyDevice model instead of the default Device one. And eventually contribute back a patch to django-fcm doc if it solves the issue.

How to set menus in Mezzanine Django

I have created a model.py file in which I define my classes like:
from django.db import models
from mezzanine.pages.models import Page
class Author(Page):
dob = models.DateField("Date of birth")
class Book(models.Model):
author = models.ForeignKey("Author")
cover = models.ImageField(upload_to="authors")
Then my HTML page and place it into templates folder define URL in urls.py file.
I run command python manage.py collecttemplates to get all templates
Now I browse 127.0.0.1/8000/page1/ to get my page view.
**Question 1: How to place this page in menu of home page using admin interface?
Question 2: How to solve this error 'NoneType' object has no attribute 'split' generates if I browse http://127.0.0.1:8000/admin/conf/setting/?
Question3 : How to access POST DATA from forms created in mezzanine interface?**
UPDATE:
from django.db import models
from mezzanine.pages.models import Page
class Author(Page):
dob = models.DateField("Date of birth")
class Book(models.Model):
author = models.ForeignKey("Author")
cover = models.ImageField(upload_to="authors")
and admin.py with these:
from django.contrib import admin
from mezzanine.pages.admin import PageAdmin
from .models import Author
admin.site.register(Author, PageAdmin)
Now i write these commands: python manage.py syncdb, python manage.py migrate,
and then open python shell to write Author.objects.create(dob = "12/12/2014")
That generates error that author is not defined. It's true because no tables created in my database.?!
I assume you're working through the Content Architecture tutorial on the Mezzanine site. The tutorial assumes a lot on your part, which is not ideal for a beginner. If you haven't seen it, you might want to take a look anyway. Here it is: http://mezzanine.jupo.org/docs/content-architecture.html
To answer Question#1: You add the new content type via the Pages admin: http://127.0.0.1:8000/admin/pages/page/ Select from the drop-down menus that read "Add..." to select its type and on the following configuration page you can select where you want it displayed as a menu link.
In response to your UPDATE:
At the Djanog/Mezzanine Python shell:
from <your app> import models
Then try models.Author.objects.create(title="Dr. Seuss")
No ideas on Questions #2 & #3 right now.

Django changing app name of Photologue

i have installed photologue - A customizable plug-in photo management application for the Django web framework here into my project without problem...
now i want to change app name in admin page which is photologue. for this i have used ugettext_lazy but i got an error when i define this to all Meta:
from django.utils.translation import ugettext_lazy as _
class Meta:
app_label = _('newappname')
Error:
ValueError at /admin/
Cannot create form field for 'effect' yet, because its related model
'PhotoEffect' has not been loaded yet
Is there any easy way for changing app name, i have looked a lot but didnt find...
Django does not support app renaming in the admin right now, but ticket #3591 was raised to add that functionality, so hopefully it will be added.
There are several ways of acomplishing that. The simplest and preferred would be changing the main admin template and using whatever name you want as the app name there.
Other solutions:
How to change the name of a Django app?
https://stackoverflow.com/a/6312798/342473
http://djangosnippets.org/snippets/1882/

How can I add dynamic field in the model in django?

I'm using django to create a application download site. I try to write a model, that the admin can add the different download content dynamically in the admin page. For example I have a software named foobar, it have 3 different version: 1.1, 1.2, 1.3. I would like the user can admin the model by using an add button to add the download link with a download version. But I don't know how to do this in django.
Set up your models to have a main model and ancillary models that have foreign keys to the main model:
class DownloadItem(models.Model):
name = models.CharField( etc etc)
... other attributes here ...
class DownloadItemFile(models.Model):
parent = models.ForeignKey('DownloadItem', related_name="versions")
version = models.CharField( etc etc)
file = models.FileField(upload='path/to/uploaddir/')
then, when you have an instance of your DownloadItem model, you can get hold of your various file versions with:
mydownloaditem.versions.all()
To be able to add files via the Admin, you will need to use an inline. In your admin.py for the app in question, you'll need to add something like:
class DownloadItemFileInline(admin.TabularInline):
model = DownloadItemFile
class DownloadItemAdminOptions(admin.ModelAdmin):
inlines = [ DownloadItemFileInline, ]
...other admin options here...
admin.site.register(DownloadItem, DownloadItem AdminOptions)

Categories

Resources