9

How would you tell Django to return ImageField.url (not relative path) when seri...

 3 years ago
source link: https://www.codesd.com/item/how-would-you-tell-django-to-return-imagefield-url-not-relative-path-when-serializing-a-model.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How would you tell Django to return ImageField.url (not relative path) when serializing a model?

advertisements

ImageField on the model

some_image = models.ImageField(upload_to= get_upload_path)

This is what i use for serializing:

data = serializers.serialize("json",ModelName.objects.all(),use_natural_keys=True)

the json i get for some_image is relative to the MEDIA_URL

to solve it manualy i call some_image.url, and that works fine, how do i make it so this will be the default value that the image returns when using serializers.serialize


Here are the docs for specifying which fields get serialized: https://docs.djangoproject.com/en/dev/topics/serialization/#subset-of-fields

It sounds like you should include url and all the other fields except some_image.

Related Articles

How would you do the The Dock icon to display a window when you clicked?

This Is Similar To One Of My Earlier Questions That Never Got Answered. I would like the Dock Icon to use the method makekeyandorderfront to open the Main window after it has been closed. I have done this with a button opening a Window but I don't kn

Analyzing the FxCop log file - how would you do it?

I'm currently writing a tool that parses the FxCop logfile on a large codebase. My goal is to provide graphical information about the most common warnings, sorted by namespace (subsystems of the application). See example graphic. This can then be use

How can you tell whether to delete a pointer returned from a function or not

Consider a function that returns a Type*, and so it looks like it could allocate a Type in its definition, but you can't tell for sure (there are many functions, and you don't have the time to read their definitions). How can you tell if you should d

How would you inherit and replace the django template classes to create a listOfStringsField object?

I want to create a new type of field for django models that is basically a ListOfStrings. So in your model code you would have the following: models.py: from django.db import models class ListOfStringsField(???): ??? class myDjangoModelClass(): myNam

Algorithm - How would you determine the minimum number of multiplications to evaluate X on power 30?

In the book Algorithms for interviewers, there is such a question: How would you determine the minimum number of multiplications to evaluate X to the power 30? Can anyone tell me what this question means? What means "evaluate X to the power 30"?

I want to write a visual studio extension to access Redmine problems. How would you recommend it?

i want to write a visual studio extension to access redmine issues. how would you recommend doing this? I thought about trying to use the work item system that already exists in visual studio with TFS, but I'm not sure that'd be the best approach or

How would you define a delayed parameter (by name) that accepts a function taking parameters?

I saw a delayed example in David Pollak's "Beginning Scala". I tried to adapt that, by trial and error. Here's what I have: def sayhello() = { println("hello") } def delaying(t: => Unit):Unit = { println("before call") t p

How would you calculate all possible permutations from 0 to N in an iterative way?

I need to calculate permutations iteratively. The method signature looks like: int[][] permute(int n) For n = 3 for example, the return value would be: [[0,1,2], [0,2,1], [1,0,2], [1,2,0], [2,0,1], [2,1,0]] How would you go about doing this iterative

How would you like EOF in bash?

I'm trying to do something like read -d EOF stdin for word in $stdin; do stuff; done where I want to replace 'EOF' for an actual representation of the end of file character. Edit: Thanks for the answers, that was indeed what I was trying to do. I act

How would you explain reflection to a non-programmer?

I have a friend who is interested in getting into programming and is asking about a lot of different concepts. Classes, interfaces and things like polymorphism / inheritence have been easy to explain, but I'm struggling a bit with analogies for refle

How would you upload files to a React-Relay application?

A file upload seems like a mutation. It's often accompanied by other data. But it's a big binary blob, so I'm not sure how GraphQL can deal with it. How would you integrate file uploads into an app built with Relay?First you need to write the Relay u

How would you generate a key that was only worth three months?

I am wondering if it is possible to generate a "key" that is valid for a period of (approximately) three months? For example, let's say (hypothetically) that I generate a key like this (pseudocode): Key = HASH ( MachineID, Salt ); And the way I

How would you get a JSONPath to all child nodes in a set of JSON objects?

How would you get a JSONPath to all child node of an object? E.g.: var data = [{ "key1": { "children": [{ "key2": "value", "key3": "value", "key4": {} }, { "key2": "value

How would you build this daily class?

What I want to do is very simple but I'm trying to find the best or most elegant way to do this. The Rails application I'm building now will have a schedule of daily classes. For each class the fields relevant to this question are: Day of the week St

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK