I am working with wagtail cms and want to use ArrayField. The default form for it is very inconvenient. I found a python package that has a better form field that is developed for Django admin. Also in the wagtail repository, I found an issue where an author states that he was able to make django-better-admin-arrayfield work with wagtail. I made several attempts but failed. Can anyone help me do it?
Update:
Model:
class Reaction(models.Model):
category = models.ForeignKey(ReactionCategory, on_delete=models.CASCADE)
name = models.CharField(max_length=128)
randomized_sentences = ArrayField(models.TextField(blank=True, null=True), null=True)
panels = [
FieldPanel('category'),
FieldPanel('name'),
FieldPanel('icon'),
FieldPanel('randomized_sentences', widget=DynamicArrayTextareaWidget)
]
ModelAdmin:
class ReactionAdmin(ModelAdmin, DynamicArrayMixin):
model = Reaction
menu_label = 'Reactions'
menu_icon = 'comment'
list_display = ('name', 'category')
search_fields = ('name', 'category__name')
I also added django_better_admin_arrayfield.css and django_better_admin_arrayfield.js to my static files directory. With all this done, The edit control for randomized_sentences looks like this: It not only looks not as expected but also adds additional '`'s in unexpected places.
Related
I am new with python and django, i want to know how can i implement the admin search bar on my project model? i notice that the user model has it by default.
My code is below, after the makemigration command still no search bar i think i am missing something. sorry for the noob question.
class Todo(models.Model):
search_fields = ('title', 'text', 'created_at',)
title = models.CharField(max_length=200)
text = models.TextField()
created_at = models.DateTimeField(default=datetime.now)
def __str__(self):
return self.title
in the apps admin.py
inside the class for which you are registering the model
add
search_fields = ['column_name']
add search fields in admin file instead of models file
Hi I am doing a project in Django 1.10. For this project I am using django-smart-select for chaining input in the admin panel.
It works fine. But for many to many fields chaining if I use filter_horizontal/filter_vertical then the chaining does not work any more.
There was no solution in there github page.
How can i solve this problem? Is there another app like this?
I have the same problem and I solved it in this fork from the library: https://github.com/jorgecorrea/django-smart-selects
As you can see in my README version, this is the way to use in horizontal mode with mi fork:
models.py
from smart_selects.db_fields import ChainedManyToManyField
class Publication(models.Model):
name = models.CharField(max_length=255)
class Writer(models.Model):
name = models.CharField(max_length=255)
publications = models.ManyToManyField('Publication',
blank=True,
null=True)
class Book(models.Model):
publication = models.ForeignKey(Publication)
writer = ChainedManyToManyField(
Writer,
horizontal=True,
verbose_name='writer',
chained_field="publication",
chained_model_field="publications",
)
name = models.CharField(max_length=255)
with this little change you can use django horizontal mode view, but do not add the field to admin filter_horizontal
this change is not needed:
admin.py
#admin.register(Book)
class BookAdmin(admin.ModelAdmin):
filter_horizontal = ('writer',)
# don't do that because you will be changing the widget.
I'm new to Django and trying to play with restframework. I have created a simple model and I'd like to POST to this model via REST and sending JSON data.
This is what I've done so far:
models.py
class Contact(models.Model):
name = models.CharField(max_length=120)
email = models.EmailField()
phone = models.CharField(max_length=15)
city = models.CharField(max_length=120)
comment = models.CharField(max_length=500)
timestamp = models.DateTimeField(auto_now_add=True)
serializers.py
class ContactSerializer(serializers.ModelSerializer):
class Meta:
model = Contact
fields = ('name', 'email', 'phone', 'city', 'comment', 'timestamp')
urls.py
url(r'^api/contact/(?P<pk>[0-9]+)/$', ContactDetail.as_view()),
views.py
class ContactDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Contact.objects.all()
serializer_class = ContactSerializer
format = None
but when I try to post to http://127.0.0.1:8001/api/contact I get this error
13. ^index.html#/verifyEmail/(?P<key>\w+)/$ [name='account_confirm_email']
14. ^api/contact/(?P<pk>[0-9]+)/$
The current URL, api/contact, didn't match any of these.
Question
How can I POST data to my model and save it?
You got a couple of problems here:
You are using generics.RetrieveUpdateDestroyAPIView which will provide the PUT, GET and DELETE methods. If you want to be able to POST (this means create) to that endpoint, you should be using another one. Replace it for viewsets.ModelViewSet, it will provide all CRUD methods. Don't forget to import the module (+more info).
You are trying to build the urls yourself which is not correct, drf provides routers to build them automatically. Just follow the docs, they are really well explained.
Once you fix those issues, you will be able to POST to /api/contact/ to create a new one.
Your main issue is that the regular expression here:
url(r'^api/contact/(?P<pk>[0-9]+)/$', ContactDetail.as_view())
does not match the URL:
http://127.0.0.1:8001/api/contact
A match should look more like the following:
http://127.0.0.1:8001/api/contact/123412213/
Including the trailing slash /
I've got a two part question regarding Django Admin.
Firstly, I've got a Django model Classified that has a foreign key field from another table Address. On setting data, I've got no issues with any of the fields and all fields get saved correctly.
However, if I want to edit the foreign field in the entry in Classified, it doesn't display the old/existing data in the fields. Instead it shows empty fields in the popup that opens.
How do I get the fields to display the existing data on clicking the + so that I can edit the correct information?
Secondly, I'm sure I've seen search fields in Django Admin. Am I mistaken? Is there a way for me to implement search in the admin panel? I have over 2 million records which need to be updated deleted from time to time. It's very cumbersome to manually go through all the pages in the admin and delete or edit those.
Adding Model Code:
Classified
class Classified(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=256)
contact_person = models.CharField(max_length=300, blank=True)
email = models.CharField(max_length=100, blank=True)
address = models.ForeignKey(Address)
subcategory = models.ForeignKey(Subcategory)
Address
class Address(models.Model):
id = models.AutoField(primary_key=True)
build_add = models.CharField(max_length=255)
street_add = models.CharField(max_length=255)
area = models.CharField(max_length=255)
city = models.ForeignKey(Cities)
The + means just that - add a new instance of the related object and relate the object you're editing to that. Because you're adding a new object it will be blank to start. If you want to be able to edit existing related objects from another object's admin you need to use inlines.
In your app's admin.py have something like:
from django.contrib import admin
from yourapp.models import Address, Classified
class AddressInline(admin.TabularInline):
model = Address
class ClassifiedAdmin(admin.ModelAdmin):
inlines = [AddressInline,]
admin.site.register(Classified, ClassifiedAdmin)
Adding search from there is really easy.
...
class ClassifiedAdmin(admin.ModelAdmin):
inlines = [AddressInline,]
search_fields = [
'field_you_want_to_search',
'another_field',
'address__field_on_relation',
]
...
Note the double underscore in that last one. That means you can search based on values in related objects' fields.
EDIT: This answer is right in that your foreignkey relationship is the wrong way round to do it this way - with the models shown in your question Classified would be the inline and Address the primary model.
Is it possible to do a reverse relation search on the Django Admin interface?
My Django app database schema consists of the following models:
class Tag(models.Model):
title = models.CharField(max_length=50)
class Publication(models.Model):
title = models.CharField(max_length=200)
tags = models.ManyToManyField(Tag, blank=True, related_name="publications")
I have added a search field for looking up tags by title in my admin.py file by doing:
class TagAdmin(admin.ModelAdmin):
list_display = ('title',)
search_fields = ('title',)
Thus, when I type a tag title into the search field on the django admin interface, a list of matching tag titles comes up. Now I'd like to make it so that if I type a tag title into the search field, matching publications come up.
In other words, I'm imagining something like:
class TagAdmin(admin.ModelAdmin):
list_display = ('title',)
search_fields = ('publications',)
Which of course doesn't work... but that's the idea...
Is this even possible? And/or am I even going about this the right way? If so, could someone suggest a way to do this or a resource? If you are kind enough to do so, please keep in mind that I am very much a beginner. Thanks.
You shouldn't try to do this using an admin class registered to your Tag model. Instead, set up an admin class for Publication and set its search_fields:
class PublicationAdmin(admin.ModelAdmin):
list_display = ('title',)
search_fields = ('tags__title',)