I'm working backend with a wagtail headless CMS and the frontend is JavaScript. I'm using a RichTextField and on the API it shows the image like this:
"<embed alt="a" embedtype="image" format="fullwidth" id="3"/>"
so because its embedtype it can't be shown on our page. I need to change the type to img with a src. I haven't tried anything because i don't even know how to start.
This is the model with the RichTextField
class ValueTagMotivation(Orderable, ClusterableModel):
"""Teams motivate why a tag was picked"""
team = ParentalKey(
TeamPage, on_delete=models.CASCADE, related_name="value_tag_motivations"
)
value_tag = models.ForeignKey(ValueTag, on_delete=models.CASCADE, related_name="+")
tagline = models.CharField(max_length=255)
motivation = RichTextField(
features=["bold", "italic", "ol", "ul", "link", "image", "embed"]
)
panels = [FieldPanel("value_tag"), FieldPanel("tagline"), FieldPanel("motivation")]
class Meta:
# a team can only pick each tag once
unique_together = (("team", "value_tag"),)
I managed to figure it out. I put
class APIRichTextSerializer(fields.CharField):
def to_representation(self, instance):
representation = super().to_representation(instance)
return expand_db_html(representation)
in my serializers.py and just called it in my main serializer
class ValueTagMotivationsSerializer(serializers.ModelSerializer):
motivation = APIRichTextSerializer()
class Meta:
model = ValueTagMotivation
fields = ['value_tag', 'tagline', 'motivation']
and now I get
"<img alt="a" class="richtext-image full-width" height="280" src="/media/images/bright-spring-view-cameo-island-260nw-10481853.width-800.png" width="475">"
Related
I am Unable to add initial/auto tags tags using tagulous by post-save signal. I am working on a project which contains smart phones Brands, models, and their specific firmware files.
What I want to do is to while posting a table I want to copy brand name model name and the files title into the tags field along with presets of tags. To do so I am using post save signals from django and it outputs the following:
I am trying to do add initial tags and also want to copy the title model brand fields to tags but code throughs below error
def tag_set(sender, instance,*arg, **kwargs):
ans= array_tag(instance)
instance.Tags.add(ans)
post_save.connect(tag_set, sender=resource)
def array_tag(instance):
return [instance.title ,instance.desc,instance.size, instance.Brand.title ,instance.Model.title ,instance.Categories.title]
Error
'list' object has no attribute 'pk'
below are the code settings i used
Models.py
class Skill(tagulous.models.TagTreeModel):
class TagMeta:
initial = [
"Python/Django",
"Python/Flask",
"JavaScript/JQuery",
"JavaScript/Angular.js",
"Linux/nginx",
"Linux/uwsgi",]
space_delimiter = False
autocomplete_view = "resource_skills_autocomplete"
class resource(models.Model):
title=models.CharField(max_length=100)
size=models.CharField( max_length=20, default="")
desc=models.TextField(default="")
file=models.FileField(default="", blank=True)
url= models.URLField(max_length=200, blank=True)
Brand = models.ForeignKey(brand,on_delete=models.CASCADE, default="")
Model = models.ForeignKey(model,on_delete=models.CASCADE, default="")
Categories = models.ForeignKey(category,on_delete=models.CASCADE, default="")
update_at=models.DateField(auto_now=True)
slug=models.SlugField(max_length=200, null=True,blank=True)
Tags = tagulous.models.TagField( to=Skill, help_text="This field does not split on spaces" )
Settings.py
SERIALIZATION_MODULES = {
'xml': 'tagulous.serializers.xml_serializer',
'json': 'tagulous.serializers.json',
'python': 'tagulous.serializers.python',
'yaml': 'tagulous.serializers.pyyaml',
}
Admin.py
class PersonAdmin(admin.ModelAdmin):
list_display = ("title","size","Brand", "Model", "Categories","Tag" )
tagulous.admin.register(models.resource, PersonAdmin)
tagulous.admin.register(models.Skill)
Forms.py
class PersonForm(forms.ModelForm):
class Meta:
fields = ["title", "Tags"]
model = models.resource
I'm currently working on a Django project, I'm new at this and it's difficult to find clear documentation about this.
My issue is very simple, I want at the creation of my model, automatically associate another model through a ManyToMany relationship.
Here goes the code :
Model :
class Favorite(models.Model):
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
title = models.CharField(max_length=60, blank=False)
estates = models.ManyToManyField(Estate, blank=True)
Serializer :
class FavoriteSerializer(serializers.ModelSerializer):
estates = EstateSerializer(read_only=True, many=True)
class Meta:
model = Favorite
fields = ['uuid', 'title', 'estates']
def create(self, validated_data):
instance = super(FavoriteSerializer, self).create(validated_data)
instance.save()
return instance
ViewSet :
class MyFavoriteEstatesListViewSet(viewsets.ModelViewSet):
serializer_class = FavoriteSerializer
def get_queryset(self):
return Favorite.objects.filter(users__id=self.request.user.id)
I'm currently sending something like this through a POST :
{"title": "some title", "estate_uuid": "XXX"}
I just wanted to instantiate my Estate model with the UUID I just sent and adding it with
favorite.estates.add(estate)
How can I achieve this ?
Thanks for your help !
I think you can try something like this:
class EstateField(serializers.RelatedField):
def to_representation(self, obj):
return EstateSerializer(obj).data
def to_internal_value(self, data):
return Estate.objects.get(pk=data)
class FavoriteSerializer(serializers.ModelSerializer):
estates = EstateField(many=True, queryset=Estate.objects.all())
class Meta:
model = Favorite
fields = ['uuid', 'title', 'estates']
I am using Custom Fields to represent the data as dictionary, but take input as uuid.
I'm having trouble finding the best way to override and add custom html to an edit/add model form in my Django admin site.
Here are the two models involved here:
Icon model used to store "Font Awesome" Icons:
class Icon(models.Model):
name = models.CharField(max_length=100, null=False)
style = models.CharField(max_length=10, choices=STYLE_CHOICES, null=False)
retired = models.BooleanField(default=False)
def delete(self):
self.retired = True
self.save()
objects = NotRetiredManager()
objects_deleted = DeletedManager()
def __str__(self):
return self.name
Workbook model that holds foreign key reference to the above Icon model:
class Workbook(models.Model):
client = models.ForeignKey(Client, on_delete=models.SET_NULL, null=True)
icon = models.ForeignKey(Icon, on_delete=models.SET_NULL, null=True, blank=True)
name = models.CharField(max_length=100)
workbookLink = models.CharField(max_length=1000)
retired = models.BooleanField(default=False)
def delete(self):
self.retired = True
self.save()
objects = NotRetiredManager()
objects_deleted = DeletedManager()
def __str__(self):
return self.name
Here are the overridden admin models for the above models:
class BaseAdmin(AdminImageMixin, admin.ModelAdmin):
def delete_queryset(self, request, queryset):
for obj in queryset:
obj.delete()
#admin.register(Workbook)
class WorkbookAdmin(BaseAdmin):
list_display = ("name", "client")
list_filter = (NameFilter, ClientNameFilter)
ordering = ("name", )
#admin.register(Icon)
class IconAdmin(BaseAdmin):
fields = ("name", "style", "icon_display")
list_display = ("icon_display", "name", "style" )
list_display_links = ("icon_display", "name")
list_filter = (NameFilter, )
ordering = ("name", )
def icon_display(self, obj):
return mark_safe(f'<i class="{obj.style}{obj.name}"></i>')
readonly_fields = ["icon_display"]
Here is a list display of some Icons I have in my database:
Currently, the add/edit page for a Workbook on my Admin Site looks like this:
I would like for that dropdown in that second screenshot to be customized similar to the "Icon Display" column in that first screenshot so that a user would choose from graphical list of icons as opposed to the default choicefield form containing the Icon names.
I've looked into the Django docs as well as similar questions on here such as this Similar Stack Overflow Question; however, I'm not fully understanding the proper way to implement something like this.
I hope the information I provided about my app is useful. Please let me know if you'd like me to provide any additional information, or add any clarifications!
I have in project two classes - Photo and Prescription inside models.py file which are related each other with foreign key. Here is part of the code:
class Photo(models.Model):
name = models.CharField(max_length=100,null=False)
photo = models.ImageField(upload_to="photos/",null=True)
def photo_tag(self):
return '<img src="/media/{0}">'.format(self.photo)
photo_tag.short_description = 'Photo of prescription'
photo_tag.allow_tags = True
class Prescription(models.Model):
description = models.CharField(max_length=100,null=True)
photo = models.ForeignKey(Photo, related_name='related_photo',null=True)
def __str__(self):
return self.description
And my Admin.py
class PhotoAdmin(admin.ModelAdmin):
list_display = ('name', 'photo_tag')
fields = ('name','photo','photo_tag')
readonly_fields = ('photo_tag',)
admin.site.register(Photo,PhotoAdmin)
class PrescriptionAdmin(admin.ModelAdmin):
list_display = ('get_photo')
fields = ('photo','description')
model = Prescription
def get_photo(self, obj):
return obj.photo.photo_tag
get_photo.short_description = 'Photo of prescription'
admin.site.register(Prescription,PrescriptionAdmin)
Question is, when I open prescriptions list instead of photo in the Photo of prescription field shows following message.
<bound method Photo.photo_tag of <Photo: Photo object>>
How real photo could be described there?
If you are going to render the image in html (which I guess you are), could you not then use ginger to display the description in a similar way to this?
<img src="{{photo.url}" title="{{photo.short_description}}" alt="{{photo.short_description}}">
Note that I included the alt for internet explorer which seems to use that instead of title.
There are few wrong issues with your approach. Your photo_tag method should be a property method, or eventually cached_property method. For displaying safely HTML code you should use the method format_html provided by Django.
Here is how I'd refactor your code:
models.py
from django.db import models
from django.utils.html import format_html
from django.utils.functional import cached_property
class Photo(models.Model):
name = models.CharField(max_length=100, null=False)
photo = models.ImageField(upload_to="photos/", null=True)
# better use blank=True instead of null=True for ImageField
#cached_property
def photo_tag(self):
if self.photo:
return format_html(
'<img src="{img}">',
img=self.photo.url
)
return None # or better return '' if you use blank=True
photo_tag.short_description = 'Photo of prescription'
class Prescription(models.Model):
description = models.CharField(max_length=100, null=True)
photo = models.ForeignKey(Photo, related_name='related_photo', null=True)
def __str__(self):
return self.description
Now you can use photo_tag as a property of Photo and bind the HTML safe in your admin.
Thanks anybody who tried to help. I figured out another solution with help of #Klaus who answered first and I am pretty sure that there is maybe much more better solutions also. Here are how I changed my code
Models.py
from django.utils.safestring import mark_safe
class Photo(models.Model):
name = models.CharField(max_length=100,null=False)
photo = models.ImageField(upload_to="photos/",null=True)
def photo_tag(self):
return mark_safe('<img src="/media/{0}">'.format(self.photo))
photo_tag.short_description = 'Photo of prescription'
photo_tag.allow_tags = True
class Prescription(models.Model):
description = models.CharField(max_length=100,null=True)
photo = models.ForeignKey(Photo, related_name='related_photo',null=True)
def __str__(self):
return self.description
Admin.py
class PhotoAdmin(admin.ModelAdmin):
list_display = ('name', 'photo_tag')
fields = ('name','photo','photo_tag')
readonly_fields = ('photo_tag',)
admin.site.register(Photo,PhotoAdmin)
class PrescriptionAdmin(admin.ModelAdmin):
list_display = ('get_photo')
fields = ('photo','description')
model = Prescription
def get_photo(self, obj):
return obj.photo.photo_tag()
get_photo.short_description = 'Photo of prescription'
admin.site.register(Prescription,PrescriptionAdmin)
Basically I have models like this:
class Playlist(models.Model):
key = models.CharField(max_length=255,blank=True, unique=True)
user = models.ForeignKey(User)
title = models.CharField(max_length=200)
pub_date = models.DateTimeField(auto_now_add=True)
videos = models.ManyToManyField(Video, through='PlaylistVideo')
class PlaylistVideo(models.Model):
playlist = models.ForeignKey(Playlist)
video = models.ForeignKey(Video)
position = models.IntegerField()
class Video(models.Model):
title = models.CharField(max_length=255,blank=True)
description = models.TextField(blank=True)
thumb = models.URLField(blank=True)
duration = models.IntegerField(default=0)
Now I want an API to return PLAYLISTS like this... But Videos should be sorted by POSITION in
PlaylistVideo Model
{
"key": "h8x3",
"title": "First Playlist",
"pub_date": "2012-10-11T17:00:26Z",
"videos": [
{
....
},
{
....
}
]
},
How should I got about it?
Not too sure if you've managed to solve your issue, but I've came across this myself, and managed to make it work like by doing something like this:
create a custom serializer like this:
class PlaylistVideoSerializer(serializers.HyperlinkedModelSerializer):
title = serializers.ReadOnlyField(video.title)
description = serializers.ReadOnlyField(video.description)
thumb = serializers.ReadOnlyField(video.thumb)
duration = serializers.ReadOnlyField(video.duration)
class Meta:
# your associative entity here
model = PlaylistVideo
fields = ('title', 'description', 'thumb', 'duration')
Here, I'm assuming you'd like to display all the fields under videos entity/table. Feel free to adjust to your needs/likings.
Then, all you need to do is this
class PlaylistSerializer(serializers.ModelSerializer):
videos = PlaylistVideoSerializer(source='playlistvideo_set', many=True)
class Meta:
model = Playlist
fields = ('key', 'title', 'pub_dates', 'videos')
Note: Always make sure that the source is referencing to the associative entity. Else you'd get a list of empty json.
Hope this would help anyone out there facing similar issue.
You can do it like this:
class PlaylistVideoList(generics.ListCreateAPIView):
serializer_class = PlaylistVideoSerializer
queryset = PlaylistVideo.objects.all().order_by('position')
in serializers.py:
class PlaylistVideoSerializer(serializers.ModelSerializer):
class Meta:
model = PlaylistVideo
We need to add some documentation on 'through' relationships really.
In the meantime, this discussion may help:
https://groups.google.com/forum/#!topic/django-rest-framework/xzOhjILq3xA/discussion