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
Related
I have a model that I only want to use one row of its table. So, on admin, I would like to remove the list and add pages, and only edit the existing object. The model is this:
from django.db import models
class Banner(models.Model):
pass
class BannerImg(models.Model):
img = models.ImageField(upload_to="banners")
banner = models.ForeignKey(Banner, on_delete=models.CASCADE)
Basically, the Banner(pk=1) will be loaded by the frontend to display a landing page hero slider. I want multiple images, but also want them to be on the same admin form, so one could order, add or remove images from the same place. Of course having to Banner objects, wouldn't make sense in this case.
I can use inline fields to do the form, but how can I achieve the pages functionality (going directly to edit)?
Thanks!
Accordion to documentation.
from django.contrib import admin
class BannerImgInline(admin.TabularInline):
model = BannerImg
class BannerAdmin(admin.ModelAdmin):
inlines = [
BannerImgInline,
]
enter image description here
Guys I hope all of you are doing great,I have a problem in django3,I have been given a problem and I have to solve it ,I have tried it but not able to solve this,see the image I have given link,
<----question starts here
This list of help1, help2,... gets stored in a database table. You could do a first version of models.py and enable the admin interface for it.
----->question ends here
Can anyone give me a idea?or how to do it or in more simpler words?any ideas or suggestions is appreciated
I'm not really understand what you mean but I think you need to create another model, let's call it Help. After created the Help model you can use it as foreignkey inside your old model.
models.py
# new model for help types
class HelpType(models.Model):
type = models.CharField(max_length=100)
def __str__(self):
return self.type
# old model
class OldModel(models.Model):
# other fields
help_type = models.ForeignKey(HelpType, on_delete=models.CASCADE)
admin.py
# register it to admin panel
from django.contrib import admin
from .models import HelpType
admin.site.register(HelpType)
Now you can create all help types in admin panel and then when you create your OldModel's object all help types will be listed as drop down in help_type field
I've two simple Django model classes,
models.py
from django.db import models
class ParentModel(models.Model):
small_text = models.CharField(max_length=20)
big_text = models.CharField(max_length=500)
def __str__(self):
return self.small_text
class ChildModel(models.Model):
parent = models.ForeignKey(ParentModel)
def __str__(self):
return '%s is my parent' % self.parent
admin.py
from django.contrib import admin
import models
admin.site.register(models.ChildModel)
admin.site.register(models.ParentModel)
So the default view is you see the 'small_text' in a select element in the admin section. What I'd love to be able to do is extend that so that there's another TextArea, or something else I can , underneath the select which changes as you choose a different Daddy.
I've looked into a few different ways to do this, but they all seem hella complicated for what with Django, I'd have thought should be an easy task. Any ideas?
If you're looking to be able to change ChildModel's properties while viewing the ParentModel in the admin, you should look into using an inline in the admin
If you're looking to have additional fields appear when viewing the index page in the admin for a model, then you'll want to add additional properties to the list_display property on the model's admin class.
I have a table that only has a handful of entries in it, and it'd be nice if I could use inlines for their list instead of forcing staff to click through to the edit page each time.
That is, when someone clicks on the link that ordinarily gives a list of the model objects, they should instead see the model objects displayed inline.
I tried something like this, but unsurprisingly it gives an error because there's no foreign key:
class MyModelInline(admin.StackedInline):
model = MyModel
class MyModelAdmin(admin.ModelAdmin):
inlines = [MyModelInline,]
admin.site.register(MyModel, MyModelAdmin)
For it to work as you've described you'll need an "editor" model to be a parent for the data. All the rows you want to display should have a foreign key to a single 'editor' model object. So, in models.py:
from django.db import models
class Editor(models.Model):
pass
class MyModel(models.Model):
name = models.CharField(max_length=100) # Field added for demonstration
# ... add any other fields you like ...
editor = models.ForeignKey(Editor)
And in admin.py:
from django.contrib import admin
from Test.models import Editor, MyModel
class MyModelInline(admin.StackedInline):
model = MyModel
class EditorAdmin(admin.ModelAdmin):
inlines = [MyModelInline,]
admin.site.register(Editor, EditorAdmin)
Some other things to consider:
When you make a new MyModel() object programmatically you must always set the foreign key to point to the editor. There should only be one instance of the editor for this to work as you've described. When using the admin interface, this foreign key should be set automatically by using the admin page for the editor object. I would suggest restricting creation and deletion of editor objects for everyone except yourself in production. If someone deletes the editor object then all MyModel objects disappear as well.
Alternative options:
1) If the edits the admin staff is doing are simple then I would recommend implementing "actions" instead.
2) There's also the possibility of overriding the admin template. I personally like this option less because every time Django is updated I have to check that my changes aren't interfering with new features. However, sometimes this is the only way to do some more advanced things in the admin interface. I've done this in my own project, but like to keep the changes minimal.
I have to create a blog posting system and I need to make it possible to upload multiple photos in Django Admin and select it via Django TinyMCE. One of the solutions is Filebrowser, but I have already spent a few days and haven't got it worked. Are there any alternatives?
I'm trying to do something similar, but not with Tinymce, I choose wmd.
I created two models, one for the blog post, and one for the images, and in the admin interface I included the images as inlines. here's some example.
in your model.py file:
class Project(models.Model):
...
#TinyMce field.
description = models.TextField()
class ProjectImage(models.Model):
image = models.ImageField(upload_to='prjimages/%Y/%m/%d/%H/%M/%S/')
project = models.ForeignKey(Project)
than in your admin.py file, you can have the PostImage as inline.
from django.contrib import admin
from models import *
class ProjectImageAdmin(admin.ModelAdmin):
pass
class ProjectImageInline(admin.StackedInline):
model = ProjectImage
max_num=10
extra=0
class ProjectAdmin(admin.ModelAdmin):
inlines = [ProjectImageInline,]
admin.site.register(ProjectImage, ProjectImageAdmin)
admin.site.register(Project, ProjectAdmin)
you can change the ImageField Widget to show the url, and maybe a preview for each image (I have no example code for this at the moment). And if the user want to include the image in the post, he can just copy paste the url to Tinymce.
It's not a complete solution, but maybe you can extend it with some work to fit your needs.
You should take look at Dropzone.js which you can easily drag and drop multiple images to admin page.