In my model I have, for now, 3 classes:
Quest: Basics attributes of quest
GoToAndAnswer: Kinda type of a quest, which extends Quest
SeekAndAnswer: Same as above, extending Quest
I'm using Django to build an API so in my View I want to print the info about these Quests in JSON format. The problem is I don't know how to serialize the objects correctly (Im really new at Django and Python):
# Only get the attributes of the class Quest, obviously
serializers.serialize("json", Quest.objects.all())
# Only get the atributes of the GoToAndAnswer, nothing about the Quest attributes
serializers.serialize("json", GoToAndAnswer.objects.all())
So which is the best way to achieve this?
I have seen posts like this: JSON Serialization of a Django inherited model
But it didnt help...
Thanks in advance
You might want to take a look at Django REST Framework which solves all your problems.
That's a Django app with a excellent documentation and all the tools you need to create a API. http://www.django-rest-framework.org/
Related
I am a beginner django developer. I am building my first serious application and I would like to use the mechanism of uploading files to the server. I have searched a large part of web, but I have not found an easy guide anywhere to deal with this issue in django2.
Is there any experienced django ninja that could provide me with a comprehensive process to design this solution, from creating the model through the form and the apparently view? I tried to work with official documentation, but I can not help it.
I will be very grateful! :D
I would like to know what gone wrong with documentation. You should be more specific when asking questions, that helps people who want to answer.
Let me try to answer your question.
You Model will simply contain an extra filefield like this
class MyModel(models.Model):
my_field = models.FileField(upload_to='your_location')
Your form will
Simply a model form which will contain this model name as class Meta
in the view part you will simply handle this as a post request but you have to add request.FILES
So it will be like
If request.method=='POST':
form = MyModelForm(request.POST, request.FILES)
Now let me come to the upload_to argument. You have to pass a string here. The file will be uploaded to that directory within media directory if the media root setup already done by you.
I am currently working to write a web app where people fill out the necessary information, and apply to their mentors.
So, at this point, mentors have a model class that is pretty much like the applicant's, so that they can correct the applicant's info without affecting the applicant's original profile.
I will appreciate any helpful comments. Specifically, I am looking for:
-A similar per-exisiting django app that does more or less so I can browse the source.
-Any special Django feature that allows this that I can not aware of.
-General info on how things like these are done in general.
Thank you.
Ad general info)
You would benefit from doing this in a single model (say ApplicationModel), with fields in pairs - field_name_applicant, field_name_mentor.
Then use a CreateView with its fields property set to only the *_applicant fields for the applicant to fill in the applications initially, and an UpdateView with its fields set to the *_mentor fields for the mentor to correct the applicant fields.
Have ApplicationModel.clean() copy all *_applicant field values to their *_mentor counterpart if the later is not set.
Now you have all your business logic in the model where it belongs; quoting a headline in the introduction of Two Scoops of Django:
Fat Models, Helper Modules, Thin Views, Stupid Templates
Im new to Django and currently building an application for logging and lending of various equipment.
I have created multiple instances of my class Equipment in the database and I want to display these in my view, preferable in a list. Additionally I would like the option to select the list objects, one at a time.
As I understand, "ModelChoiceField" gets the job done, but I just don't understand how to implement it. I've read the specific documentation, but yet I've been failing for many hours and I feel really stupid.
So if someone would be so kind to give me a generic example of how to implement ModelChoiceField throughout veiws.py, forms.py, templates.html etc. I'd be very grateful!
forms.py:
class MyForm(forms.Form):
# or with some filter applied
my_field = forms.ModelChoiceField(queryset=Equipment.objects.all())
Then just use {{ form.as_<whatever you want> }} in your template and it should work.
I'm porting a project from Rails to Django with a legacy database. In Rails I had a polymorphic association that allowed me to add a footnote to any row in the database. I'm trying to implement the same thing in the Django app. I found the documentation on Generic Relations and it looks perfect. Unfortunately, I first need to create new fields in my legacy database to hold the ContentType id for the relevant models. I only used the polymorphic association with 2 tables, so all I need are those two corresponding ids from the Django app, but I can't seem to find the appropriate command for looking up a ContentType id in Django.
Any suggestions are most welcome. I tried searching through previous questions but couldn't seem to find what I am looking for. Thank you very much for you time and help.
from the docs
you can do:
>>> b = Bookmark.objects.get(url='https://www.djangoproject.com/')
>>> bookmark_type = ContentType.objects.get_for_model(b)
>>> TaggedItem.objects.filter(content_type__pk=bookmark_type.id,
... object_id=b.id)
so just instantiate an instance of your model and then do ContentType.objects.get_for_model(<instance>).id
I think there's a way to pass just the model name, too... let me know if that would work better and I'll try to find it; I've used it in the past.
You can also get ContentType ID without creating an instance, which is more efficient if you don't have and don't need an instance but just want to get ContentType ID by model name.
ContentType.objects.get(model='bookmark').id
Notes : if the name of your model is upper case, please use lower case to search. For example: model = 'bookmark' rather than 'Bookmark'
Would it be possible/best practice to use 1 FormWizard for manipulating multiple models?
I've experimented with the FormWizard and have defined all the forms. The page 'flow' itself works like a charm. However with all the checks that need to be done and Models that are manipulated it feels like I'm sticking code in the form's __init__ and/or process_step() that really belongs in views.py. The docs even state: "Dont manipulate form data via process_step().
Testing a concept with everything in one view, thus using a last_submitted_page (the step equiv) it feels like I'm writing another formwizard.
Anybody been here before? Tips welcome.
Thanx a lot.
Regards,
Gerard.
Zooming out on the issue I decided to go for the FormWizard approach. I re-confirmed that code is easily placed in forms.py and that you can get your model info by using. the process_step().
GrtzG