6

I would like to do the following:

With my model

class User(models.Model):
   id = models.AutoField(primary_key=True)
   field1 = models.CharField(max_length=100)
   fk1 = models.ForeignKey(Group)
goes on

After this, I created my Serializer, which looks like:

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserSerializer
        fields = (...)

    ...

Lastly, I create my ViewSet, which should look like this:

class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer

but now, how would I be able to create a viewSet and, for example, generate a form with my class? My final idea is to be able to do something like:

def my_view(request):
   my_form = UserViewSet.as_view({'get': 'list'}(request))
   # from here either to be able to use .render().content
   # or, inside the template, render with {% render_form my_form %}

Is this possible? Thank you

2 Answers 2

2

What you're suggesting is possible, but it doesn't usually make sense. Django Rest Framework is great for creating an API so that you can send and receive information to and from that api, maybe in JSON format.

Getting a form involves getting rendered HTML from the server. That's a better task for Django itself. You could use a CreateView to do what you're trying to:

def UserCreateView(CreateView):
    model = User
    form_class = "name_of_your_form.html"  # Or you could call the form 'user_create_form.html` and leave this line out.
    fields = [...]

Your form will then be available in your template, per the docs:

<form action="" method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Save" />
</form>
Sign up to request clarification or add additional context in comments.

1 Comment

Can you suggest me a better way to do so? I am open to good practices.. I am just starting to learn it.. I need to keep serializers though
2

As docs says:

Serializers may be rendered as forms by using the render_form template tag, and including the serializer instance as context to the template.

Next example will probably work for you. But how @YPCrumble sayed DRF goal is easy and fast build an API, not rendering forms

class UserDetail(APIView):
    renderer_classes = [TemplateHTMLRenderer]
    template_name = 'your_template.html'

    def get(self, request, pk):
        serializer = UserSerializer(profile)
        return Response({'serializer': serializer})

    def post(self, request, pk):
        user = get_object_or_404(User, pk=pk)
        serializer = UserSerializer(data=request.data)
        if not serializer.is_valid():
            return Response({'serializer': serializer})
        serializer.save()
        return redirect('some_url_name')  

template file:

{% load rest_framework %}
<html><body>

   <form action="{% url 'user_url' pk=user.pk %}" method="POST">
        {% csrf_token %}
        {% render_form serializer %}
        <input type="submit" value="Save">
    </form>

</body></html>

`DRF Docs link

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.