Re: Serializing and abstract classes

2019-09-18 Thread Jani Tiainen
Hi.

You're using table inheritance which by default is like one way road.

In Question model there exists two reverse relations to your child models.
You just need to test which one is set.

If you truly want abstract base class (which changes model layouts as well)
you need to make Question model abstract in Meta.


to 19. syysk. 2019 klo 0.20 Courtney Bracefield <
courtneybracefiel...@gmail.com> kirjoitti:

> Hi there,
>
> I have some classes in my model.py file shown below. I would like my API
> to return a list of questions showing the slug, title and question_type of
> each question. Fetching the slug and title is trivial, but I cannot for the
> life of me access the question_type of a question because it isn't under
> the Question class, it is a field under each subclass (shown below).
>
> Anyone have any ideas? Thanks!
>
> class Question(TranslatableModel):
> """Base class for a question.
>
> Aims to be an abstract class as questions should be
> a particular subtype, though the class is not made
> completely abstract to allow retrieving all child
> objects through the InheritanceManager.
> """
>
> slug = models.SlugField(unique=True)
> title = models.CharField(max_length=SMALL)
> objects = InheritanceManager()
>
>
> class QuestionTypeProgram(Question):
> """A program based question."""
>
> QUESTION_TYPE = 'program'
>
>
> class QuestionTypeFunction(Question):
> """A function based question."""
>
> QUESTION_TYPE = 'function'
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django REST framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-rest-framework+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-rest-framework/6da5f79c-5a51-4fe5-aa34-64a96b3571b6%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-rest-framework/CAHn91odLS0A-1fdtkpe0HBEGzRsrwH-6wZ%3Dv7bbajSg96-XjGA%40mail.gmail.com.


Re: Documentation for "Tutorial 1: Serialization" need to another version for Windows OS

2019-03-13 Thread Jani Tiainen
Hi.

You might want to checkout pipenv tool. It handles nicely both virtualenv
and package installation.


ma 11. maalisk. 2019 klo 13.39 Edely Gomes  kirjoitti:

> Hey, Trong.
>
> About your virtual environment, in Windows usualy you activate the env
> with a command like
>
> .\env\Scripts\activate
>
> No need for `python -m source`
>
> If you are using Linux, you would do
>
> source /env/bin/activate
>
> Are you sure you are activating your environment?
>
> Em domingo, 10 de fevereiro de 2019 11:09:19 UTC-3, trong vo nhu escreveu:
>>
>> Hi everyone 🙋‍♂️,
>>
>> I'm a newbie of Python and Django framework. While learning with tutorial
>> in www.django-rest-framework.org, I experienced with many promblems
>> because I am using Windows OS.
>> I tried to find the feedback button for the documentation update and
>> didn't find it.
>> So I want to know where can I give the feedback for those issue, in my
>> case they are corresponding commands with Windows OS.
>>
>> Example: in this tutorial Tutorial 1: Serialization
>> 
>> These two commands:
>> virtualenv env
>> source env/bin/activate
>> should be (I'm using python 3.7 64bit, Windows 10):
>> python -m source env\Scripts\activate
>> env\Scripts\activate
>>
>> Sorry for my bad English.
>> Thank you very much.
>> Trong
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django REST framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-rest-framework+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Get user password from JWT

2019-01-18 Thread Jani Tiainen
Hi.

Note that JWT is not encryption but cryptography signing. So password would
be in clear plaintext in JWT payload. Also storing password in session
might not be a good thing depending where your sessions are stored since
again, password would be plain text there as well.

If you really need to use plain text password storing a secondary "I don't
care what happens with this password" would be better solution to access
remote services.


On Fri, Jan 18, 2019 at 2:33 AM Wandss  wrote:

> Hi everyone,
>
> I'd like to know if anyone could point me to a solution to a problem that
> seems to be easy.
>
> I've built an API and I'm authenticating users with djangorest_jwt.
> After user has been authenticated, I'll have to "programmatcially" consume
> another API which I connect with requests using BasicAuthentication,
> meaning I'll have to pass to this other API the users's plain text password.
>
> Using only Django I could store user's password into a session variable
> and retrieve it later on any other view.
>
> How could I solve this situation without sessions?
> Has anyone ever experienced this situation?
>
> Regards you all and
> Happy coding !!!
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django REST framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-rest-framework+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>


-- 
Jani Tiainen

- Well planned is half done, and a half done has been sufficient before...

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Information

2019-01-08 Thread Jani Tiainen
Hi.

Before jumping into more advanced stuff, what problem you do have that DRF
would resolve for you?

Because building a dashboard like that doesn't require DRF, you can do it
very well with Django itself without any additional packages.

Also what do you mean with "authentication in a secure way"? What would be
insecure?

On Tue, Jan 8, 2019 at 12:08 PM  wrote:

> Hello, I started in Django and I would like to know if Django REST
> framework could meet my needs:
>
>
>- Manages authentication in a secure way (admin & user) ?
>- Have a vertical dashboard as on this image (user & admin, of course
>the admin will have more functionality) ?
>
> [image: 2019-01-07_16h17_25-ConvertImage.jpg]
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> Or better to go on another package of its kind django-control
> <https://django-controlcenter.readthedocs.io/en/latest/> ?
>
> Thank you, you would help me a lot by answering it is question. :)
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django REST framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-rest-framework+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>


-- 
Jani Tiainen

- Well planned is half done, and a half done has been sufficient before...

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: How to customize table name for model classes and still use django managed tables

2018-08-21 Thread Jani Tiainen
Hi,

db_table in model Meta should definitely change table name (after
migrations of course). Maybe you made some kind of a typo and it didn't
worked.


On Tue, Aug 21, 2018 at 10:09 AM, Alexander Lamas <
alexander.g.la...@gmail.com> wrote:

> Hi All,
>
> I'm not sure if this is a discussion for this group.
>
> I wonder if there is a way to stop django/python from adding the prefix
> "myapp" for all tables it creates when migrating.
>
> I managed to use db_column='ColumnName' and now my tables have the column
> name I want, but, using db_table inside of my Meta class doesn't help
> anything.
>
> I still want to keep Django to manage everything, but, not the table
> naming. :)
>
> Thanks in advance!
>
> Alex
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django REST framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-rest-framework+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Jani Tiainen

- Well planned is half done, and a half done has been sufficient before...

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Authentication for Anonymous users

2018-04-20 Thread Jani Tiainen
Hi,

Sure you could but use would be that? It's just a string describing user
agent. And it may be a lie.

You do have a few options here:

Simplest is that you really don't care about "anonymous" access or it's
usage limits. You may impose some throttling to avoid hammering of your
service.

Another option is to use a long term session to store number of accesses
and don't care about people who reset their sessions.

Finally you can make people to login with some "simple" authorization
mechanism like social site logins.


On Fri, Apr 20, 2018 at 3:23 PM, Musharaf Baig 
wrote:

> Can we make authentication on User-Agent coming in request?
>
> On Friday, April 20, 2018 at 3:36:45 PM UTC+5, Jani Tiainen wrote:
>>
>> Hi,
>>
>> 1) How you control that user (without login) has expire his/her
>> number/time on your site?
>>
>> 2) Have users (internal, even you don't have registration or login) that
>> you assign permanent token and use it as an auth. Django Rest Framework
>> supports it out of the box.
>>
>> On Thu, Apr 19, 2018 at 10:43 PM, Musharaf Baig 
>> wrote:
>>
>>> I *do not* have register/login mechanism for the users.
>>>
>>> I need two types of authentication:
>>>
>>>1.
>>>
>>>To make it available I would like to let the user use it for free
>>>with a limited number of call s or time
>>>
>>> - for example after the API is called 50 times, I would like that the
>>> token expires. An alternative solution is that  the token should expire in
>>> 2 days.
>>>
>>> 2) An authentication with a key that does not expire at all.
>>>
>>> Is it possible? Need suggestions. Thanks
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Django REST framework" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to django-rest-framework+unsubscr...@googlegroups.com.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>
>>
>> --
>> Jani Tiainen
>>
>> - Well planned is half done, and a half done has been sufficient before...
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django REST framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-rest-framework+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Jani Tiainen

- Well planned is half done, and a half done has been sufficient before...

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Authentication for Anonymous users

2018-04-20 Thread Jani Tiainen
Hi,

1) How you control that user (without login) has expire his/her number/time
on your site?

2) Have users (internal, even you don't have registration or login) that
you assign permanent token and use it as an auth. Django Rest Framework
supports it out of the box.

On Thu, Apr 19, 2018 at 10:43 PM, Musharaf Baig 
wrote:

> I *do not* have register/login mechanism for the users.
>
> I need two types of authentication:
>
>1.
>
>To make it available I would like to let the user use it for free with
>a limited number of call s or time
>
> - for example after the API is called 50 times, I would like that the
> token expires. An alternative solution is that  the token should expire in
> 2 days.
>
> 2) An authentication with a key that does not expire at all.
>
> Is it possible? Need suggestions. Thanks
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django REST framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-rest-framework+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Jani Tiainen

- Well planned is half done, and a half done has been sufficient before...

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: csrf token for put and patch forms

2018-03-09 Thread Jani Tiainen
Note that you can use HTML form only to issue GET or POST.

Other verbs do require xhr (ajax) calls. There is an example in Django csrf
docs how to pass token in xhr headers.

9.3.2018 17.22 "Jochen Wersdoerfer" 
kirjoitti:

> Hi *,
>
> I'm trying to use custom put forms for the browsable api. In my
> development environment
> everything worked as expected, but in production I got csrf errors on
> submitting those put
> forms (csrf token missing or incorrect). So I looked at base.html and
> learned that only post
> forms get a {% csrf_token %}. Then I used api.html to overwrite the body
> block with a version
> that adds {% csrf_token %} to put forms, but it didn't work. I still get
> csrf errors and I'm
> wondering whether the csrf_token tag was left out intentionally or if it
> is a bug. Maybe someone
> knows something about this?
>
> best,
> Jochen
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django REST framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-rest-framework+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: How to not allow additional properties

2018-01-15 Thread Jani Tiainen
Hi,

Is there a reason you want to reject requests with extra data rather than
just ignore extra values?

One way to do that could be whitelisting allowed params per view but that
you have to do yourself. You could probably use DRF authentication API for
that.

15.1.2018 18.43 "Daniel Chimeno"  kirjoitti:

> Hello!
>
> How could I deny requests that has additional properties?
> Example:
>
> class IntervalSerializer(serializers.Serializer):
> month = serializers.DateField(format='%Y-%m', input_formats=['%Y-%m'])
> day_of_week = serializers.IntegerField(min_value=1, max_value=7)
> start_hour = serializers.IntegerField(min_value=0, max_value=24)
> end_hour = serializers.IntegerField(min_value=1, max_value=24)
>
> I would like to allow incoming requests like:
> {'month': '2017-03', 'day_of_week': 7, 'start_hour': 3, 'end_hour': 2}
>
>
> but to not allow incoming requestse like:
> {'month': '2017-03', 'day_of_week': 7, 'start_hour': 3, 'end_hour': 2,
> 'another_attribute': 3}
>
>
> Thanks!
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django REST framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-rest-framework+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Current I18n strategy seems awfully fragile

2017-11-02 Thread Jani Tiainen

Hi,

This is how things are done with gettext [1] in general and Django (and 
pretty much it's whole ecosystem) just has adopted that convention.


Having abstract keys instead of plain English isn't problem free either, 
they just have a different problems.


Note that you can have Django gettext contextual markers to avoid 
translation collisions in your translations. [2]


1. 
https://www.gnu.org/software/gettext/manual/html_node/Preparing-Strings.html#Preparing-Strings
2. 
https://docs.djangoproject.com/en/1.11/topics/i18n/translation/#contextual-markers


On 2.11.2017 12.45, Steve Jorgensen wrote:
Well, convention or not, can you see why that is a ridiculously 
fragile convention? From what you're saying, this is how things are 
done in Django-land though, so it would be awkward for DRF to do 
something different. Poop. :(


On Wednesday, November 1, 2017 at 11:16:26 PM UTC-7, Jani Tiainen wrote:

Hi,

Django and DRF both uses GNU gettext to handle translations. With
gettext it's convention is to use plain English "message id"
instead of what Ruby does.

Ruby seems to have it's own translation mechanisms which is
tailored to be used with abstract message id's.

2.11.2017 2.23 "Steve Jorgensen" >
kirjoitti:

Looking at
http://www.django-rest-framework.org/topics/internationalization/
<http://www.django-rest-framework.org/topics/internationalization/>
and

https://raw.githubusercontent.com/encode/django-rest-framework/master/rest_framework/locale/en_US/LC_MESSAGES/django.po

<https://raw.githubusercontent.com/encode/django-rest-framework/master/rest_framework/locale/en_US/LC_MESSAGES/django.po>


I see that each msgid value is a full English language
message. That means that if an application has its own
translation(s) for DRF's intrinsic messages, they can break if
the wording of the messages changes from one DRF to the next.
I come from the Rails world where the IDs are symbols that do
not change, and the actual text for the message in any
language is determined through I18n.
Is there some way this could be improved in future versions of
DRF?
-- 
You received this message because you are subscribed to the

Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from
it, send an email to
django-rest-framework+unsubscr...@googlegroups.com .
For more options, visit https://groups.google.com/d/optout
<https://groups.google.com/d/optout>.

--
You received this message because you are subscribed to the Google 
Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send 
an email to django-rest-framework+unsubscr...@googlegroups.com 
<mailto:django-rest-framework+unsubscr...@googlegroups.com>.

For more options, visit https://groups.google.com/d/optout.


--
Jani Tiainen

--
You received this message because you are subscribed to the Google Groups "Django 
REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Current I18n strategy seems awfully fragile

2017-11-01 Thread Jani Tiainen
Hi,

Django and DRF both uses GNU gettext to handle translations. With gettext
it's convention is to use plain English "message id" instead of what Ruby
does.

Ruby seems to have it's own translation mechanisms which is tailored to be
used with abstract message id's.

2.11.2017 2.23 "Steve Jorgensen"  kirjoitti:

> Looking at http://www.django-rest-framework.org/topics/
> internationalization/ and https://raw.githubusercontent.com/encode/
> django-rest-framework/master/rest_framework/locale/en_US/
> LC_MESSAGES/django.po
>
> I see that each msgid value is a full English language message. That
> means that if an application has its own translation(s) for DRF's intrinsic
> messages, they can break if the wording of the messages changes from one
> DRF to the next.
>
> I come from the Rails world where the IDs are symbols that do not change,
> and the actual text for the message in any language is determined through
> I18n.
>
> Is there some way this could be improved in future versions of DRF?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django REST framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-rest-framework+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: import DjangoREST+Geodjango

2017-06-13 Thread Jani Tiainen
Hi.

Next time please copy-paste text since it's really hard to response to
images.

Problem is that there is no such app in django as django.contrib.gis.gdal.
It's just a library.

You only need to add django.contrib.gis to installed apps to leverage gis
functionality.


13.6.2017 22.31 "Gabriel Sansigolo"  kirjoitti:

> Hello everyone, my name is Gabriel Sansigolo. Last week I came across a
> little problem during the development of a project.
>
> The problem appeared when I needed to import a function from a class,
> which was in another folder.
> Captura_1: Organização dos Arquivos 
>
> At first I thought it was wrong in the way I wrote import.
> Captura_2: O Erro 
>
> The source pointed to by the error was the urls.py file, as the image
> shows:
> Captura_3: urls.py 
>
> It should be noted that PyCharm did not identify it as an error. After
> researching on the subject I noticed that many said that probably the
> problem was in the "INSTALLED_APPS" inside the settings.py file, thinking
> about it follow image of my installed apps:
> Captura_4: Installed Apps 
>
> his may be a trivial mistake in Django, but as I do not know much, I'm a
> beginner, I can not see the solution.
> Links of the prints, for those who do not appear the hyperlinks:
>
> Http://imgur.com/deEWMqn
> Http://imgur.com/F9kPZ0W
> Http://imgur.com/kR7PUHI
> Http://imgur.com/zsM3qBo
>
> Thanks for the space.
>
> regards.Gabriel
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django REST framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-rest-framework+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: How can I raise custom Exception in validate

2017-06-12 Thread Jani Tiainen

Hi,

To me it looks like you're doing wrong solution for your problem.

Serializer is mainly meant to convert data from complex format to python 
primitives and vice versa.


Validation is to make sure that converted (deserialized) object matches 
the definition of the serializer.


Now, to me it looks like you want to check is something authenticated. 
That's not exactly job for serializer,
but job of your view code (one that calls deserialization) after object 
has been successfully deserialized and validated.



On 12.06.2017 13:30, Sinan Ülker wrote:
Is there any example of custom handling in serializer. I couldn't 
figure out how to do that.


On Jun 12, 2017 09:19, "Jani Tiainen" <mailto:rede...@gmail.com>> wrote:


Hi,

You need to raise validation error (or subclass of it).

Another option is to move authentication check outside serializer
(which might be right thing to do)
and catch that error properly there (or do custom handling).


On 12.06.2017 04:57, Sinan Ülker wrote:

Hello,

Since serializer.is_valid()  does not catch the Exceptions other
than the validationError, how can i raise custom exceptions in
validate method and then catch get them with serializer.errors

for example:

class  Foo(serializers.Serializer):
 def  validate(self,data):
 raise  exceptions.AuthenticationFailed("message")
 then I want to test it like this but serializer.is_valid()
line always return error.
serializer = Foo(some.data)
is_valid = serializer.is_valid()
expected_error = {['message.']}
self.assertFalse(is_valid)
self.assertEqual(serializer.errors, expected_error)

I also could not figure out how to handle it with custom
exception handler can you help me about this. I also appreciate
if you supply some examples

-- You received this message because you are subscribed to the
Google Groups "Django REST framework" group. To unsubscribe from
this group and stop receiving emails from it, send an email to
django-rest-framework+unsubscr...@googlegroups.com
<mailto:django-rest-framework+unsubscr...@googlegroups.com>. For
more options, visit https://groups.google.com/d/optout
<https://groups.google.com/d/optout>. 


-- 
Jani Tiainen


-- You received this message because you are subscribed to a topic
in the Google Groups "Django REST framework" group. To unsubscribe
from this topic, visit

https://groups.google.com/d/topic/django-rest-framework/8Ai8Vrss0Gg/unsubscribe

<https://groups.google.com/d/topic/django-rest-framework/8Ai8Vrss0Gg/unsubscribe>.
To unsubscribe from this group and all its topics, send an email
to django-rest-framework+unsubscr...@googlegroups.com
<mailto:django-rest-framework+unsubscr...@googlegroups.com>. For
more options, visit https://groups.google.com/d/optout
<https://groups.google.com/d/optout>. 

-- You received this message because you are subscribed to the Google 
Groups "Django REST framework" group. To unsubscribe from this group 
and stop receiving emails from it, send an email to 
django-rest-framework+unsubscr...@googlegroups.com 
<mailto:django-rest-framework+unsubscr...@googlegroups.com>. For more 
options, visit https://groups.google.com/d/optout. 

--
Jani Tiainen

--
You received this message because you are subscribed to the Google Groups "Django 
REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: How can I raise custom Exception in validate

2017-06-11 Thread Jani Tiainen

Hi,

You need to raise validation error (or subclass of it).

Another option is to move authentication check outside serializer (which 
might be right thing to do)

and catch that error properly there (or do custom handling).


On 12.06.2017 04:57, Sinan Ülker wrote:

Hello,

Since serializer.is_valid()  does not catch the Exceptions other than 
the validationError, how can i raise custom exceptions in validate 
method and then catch get them with serializer.errors


for example:

class  Foo(serializers.Serializer):
 def  validate(self,data):
 raise  exceptions.AuthenticationFailed("message")
 then I want to test it like this but serializer.is_valid() line 
always return error.

serializer = Foo(some.data)
is_valid = serializer.is_valid()
expected_error = {['message.']}
self.assertFalse(is_valid)
self.assertEqual(serializer.errors, expected_error)



I also could not figure out how to handle it with custom exception 
handler can you help me about this. I also appreciate if you supply 
some examples





--
You received this message because you are subscribed to the Google 
Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send 
an email to django-rest-framework+unsubscr...@googlegroups.com 
<mailto:django-rest-framework+unsubscr...@googlegroups.com>.

For more options, visit https://groups.google.com/d/optout.


--
Jani Tiainen

--
You received this message because you are subscribed to the Google Groups "Django 
REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django rest framework request data is always null

2017-03-13 Thread Jani Tiainen

Hi,

Looks like your code that calls POST data is incorrect. As you see in 
printout for some reason included data is strangely wrapped as an object 
to toplevel item_id.



On 10.03.2017 15:27, Sandesh Naroju wrote:
I just want to try simple Django rest framework code, I used APIVIEW 
and posted data through browser.


CODE:

class GetData(APIView):

def post(self, request, format=None):
if request.method == 'POST':
item_id = request.data.get('item_id')
return Response({"return": "OK ","item_id":item_id})
else:
return Response({"return": "NOT OK "})


The above code should echo the item_id whatever i "POST"ed. but the 
result is always null.


POST data:

{
 "item_id" : "3456787"
}

result:

{
"item_id":null,
"return" :"OK "
}

I don't understand where I went wrong. the above code works fine in my 
loaclhost but does not work in production server.


I tried to print request.data which gives me this

{ "item_id": { "_content_type": "application/json", "_content": 
"{\r\n\r\n\"item_id\" : \"3456787\"\r\n\r\n}" }, "return": "OK " }


--
You received this message because you are subscribed to the Google 
Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send 
an email to django-rest-framework+unsubscr...@googlegroups.com 
<mailto:django-rest-framework+unsubscr...@googlegroups.com>.

For more options, visit https://groups.google.com/d/optout.


--
Jani Tiainen

--
You received this message because you are subscribed to the Google Groups "Django 
REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Apps aren't loaded yet, from rest_framework.authtoken.models import Token

2017-01-30 Thread Jani Tiainen

Hi,

Don't import models in models.py since at point of import there is no 
guarantee that model is actually registered, and thus you get the exception.



To fix the issue use string format to refer models in other apps. In 
your case instead of importing model and doing the following:


token = models.ForeignKey(Token)

You should do (check app name, I just guessed it):

token = models.ForeignKey('authtoken.Token')

(and remove the import all thogether)

On 30.01.2017 10:50, Soham Navadiya wrote:


I have upgraded django 1.7 to 1.9.12 and django-rest-framework 3.5.3

I am getting below error stack after running the server.
I have tried to apply various thing. but It doesn't help.
How can I solve this?

|File "/home/soham/Documents/upg/rest_api/api/models.py", line 7, in 
 from rest_framework.authtoken.models import Token File 
"/home/soham/Documents/upg/rest_api/cubii_d_u/local/lib/python2.7/site-packages/rest_framework/authtoken/models.py", 
line 11, in  class Token(models.Model): File 
"/home/soham/Documents/upg/rest_api/cubii_d_u/local/lib/python2.7/site-packages/django/db/models/base.py", 
line 94, in __new__ app_config = 
apps.get_containing_app_config(module) File 
"/home/soham/Documents/upg/rest_api/cubii_d_u/local/lib/python2.7/site-packages/django/apps/registry.py", 
line 239, in get_containing_app_config self.check_apps_ready() File 
"/home/soham/Documents/upg/rest_api/cubii_d_u/local/lib/python2.7/site-packages/django/apps/registry.py", 
line 124, in check_apps_ready raise AppRegistryNotReady("Apps aren't 
loaded yet.") django.core.exceptions.AppRegistryNotReady: Apps aren't 
loaded yet.|

--
You received this message because you are subscribed to the Google 
Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send 
an email to django-rest-framework+unsubscr...@googlegroups.com 
<mailto:django-rest-framework+unsubscr...@googlegroups.com>.

For more options, visit https://groups.google.com/d/optout.


--
Jani Tiainen

--
You received this message because you are subscribed to the Google Groups "Django 
REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Newbie: how to debug POST server response 403?

2017-01-25 Thread Jani Tiainen

Hi,

It is not enought that you send token as a cookie, you need to send 
cookie value as part of the POST request as well.


Django uses X-CSRFToken header in AJAX calls. You could use normal POST 
variable if you're using normal form encoded POST, but usually with DRF 
you send something like JSON where it's not possible to embed CSRF token.


See https://docs.djangoproject.com/en/1.10/ref/csrf/#ajax  for more 
information and example with jQuery.



On 26.01.2017 08:47, Bob Aalsma wrote:

Hi Jani,

Thanks!

I've checked now (learning as well, thank you :)) and I see the 
response from the server but the body of the response says "failed to 
load response data".


I've tested what happens when this user is not logged in: same error.
[I had taken 403 to mean that the user was authenticated but not 
permitted to perform the requested operation - not so!]
After logging in again I can see the csrf token as a cookie; the csrf 
token is included in the send but I'm not sure how to verify the 
correctness of that part.

Is there a way to see if this is the cause of the problem?
Bob


--
You received this message because you are subscribed to the Google 
Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send 
an email to django-rest-framework+unsubscr...@googlegroups.com 
<mailto:django-rest-framework+unsubscr...@googlegroups.com>.

For more options, visit https://groups.google.com/d/optout.


--
Jani Tiainen

--
You received this message because you are subscribed to the Google Groups "Django 
REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Newbie: how to debug POST server response 403?

2017-01-25 Thread Jani Tiainen
Hi,

Have you checked from your browser debugging tools (network tab or similar)
what's the body of the response? it should tell more detailed reason of an
error.

Convenient place would be views dispatch() method where most of the magic
happens, though usually it's enough to check response body in browser,

My educated guess is that you're missing CSRF token when doing POST.

On Wed, Jan 25, 2017 at 4:39 PM, Bob Aalsma 
wrote:

> Hi,
>
> I'm new to Django Rest Framework (and new to Javascript), not new to
> Python, not really now to Django.
>
> I've created a Javascript that should send a POST request to
> localhost/Django Rest framework app.
>
> I've copied the contents of the message from Javascript console.log into
> the Rest API html - OK
> I've copied the message to a POST through http - OK:
>
> HTTP/1.0 201 Created
>
> BUT when I send the POST from the Javascript to the server, it says
>
> "POST /texts/texts/ HTTP/1.1" 403 58
>
> So I'd want to get a closer look at what happens with the incoming message
> and I'd normally put a "import ipdb; ipdb.set_trace()" at a convenient
> place.
> And now I discover that I still don't understand enough of this framework
> to decide on "a convenient place"...
> Please help.
>
> Regards,
> Bob
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django REST framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-rest-framework+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Jani Tiainen

- Well planned is half done, and a half done has been sufficient before...

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Wrapping a SOAP API in REST

2016-10-26 Thread Jani Tiainen

Hi,

You're really trying to mix apples and oranges.

SOAP, is it says is "simple object access protocol" which is mainly 
meant to do RPC calls.


Now RESTful is just an architecture.

You just need to map several RPC calls to REST calls and you're done.

Calling SOAP from Python isn't easy task since depending on end SOAP 
point some libs do work better than other.


And in worst case I've seen so poorly generated SOAP that none of the 
Python SOAP libs couldn't talk to that.


So you have to think SOAP being something that you just call like 
functions in Python to return you some data. Which you then transform in 
proper format with DRF.


On 11.02.2015 14:43, Tharshan wrote:

Hi,

I was hoping this group could give me some advice on what the best way 
to approach wrapping a SOAP API through DRF and expose the resources 
found in the SOAP Schemas as REST endpoints. One of the issues is the 
creation of the classes and fields required for a each resource. Is 
there a way to generate classes by parsing the XML Schema of the SOAP 
API? Would you write a serialiser that translated back and forth 
between XML? To send the response in XML but also to receive a 
response in XML but send it back to the client as JSON.


Any ideas or suggestions are welcome.

Thanks
--
You received this message because you are subscribed to the Google 
Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send 
an email to django-rest-framework+unsubscr...@googlegroups.com 
<mailto:django-rest-framework+unsubscr...@googlegroups.com>.

For more options, visit https://groups.google.com/d/optout.


--
Jani Tiainen

--
You received this message because you are subscribed to the Google Groups "Django 
REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: PUT - Update on a M2M Field -- it can't be this hard can it?

2016-07-29 Thread Jani Tiainen

Hi,

You probably want to use PATCH instead of PUT. Since traditionally PUT 
means actually replace item or collection, not add.


Another option could be defining shopping cart so that POST to cart is 
used to add new items to cart.


Of course can't tell would it work automatically on DRF.

On 28.07.2016 21:04, Ryan Castner wrote:

# models.py

class ShoppingCart(models.Model):
owner = models.OneToOneField(settings.AUTH_USER_MODEL)Hi,
items = models.ManyToManyField(Item)


class Item(models.Model):
name = models.CharField()


# serializers.py

class ShoppingCartSerializer(serializers.ModelSerializer):
items = serializers.PrimaryKeyRelatedField(many=True, 
queryset=Item.objects.all())

class Meta:
model = ShoppingCart
fields = ('owner','items')

# views.py

class ShoppingCartDetail(APIView):
def get(self, request, pk, format=None):
shopping_cart = request.user.shoppingcart
serializer = ShoppingCartSerializer(shopping_cart)
return Response(serializer.data)

def put(self, request, pk, format=None):
shopping_cart = request.user.shoppingcart
data = request.data
data['owner'] = request.user.pk
reg_dict = dict(data.iterlists())
for item in shopping_cart.items.all():
pk = unicode(str(item.pk), "utf-8")
if pk not in reg_dict['items']:
reg_dict['items'].append(pk)

query_dict = QueryDict('', mutable=True)
query_dict.setlist('owner', reg_dict['owner'])
query_dict.setlist('items', reg_dict['events'])
serializer = ShoppingCartSerializer(shopping_cart, 
data=query_dict)

if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, 
status=status.HTTP_400_BAD_REQUEST)


# all of that craziness just so a ShoppingCart with ['owner': 1, 
'items': [1]] can be updated with a PUT of ['owner': 1, 'items': 2] to 
be ['owner':1, 'items': [1,2]]


essentially I just wanted to ADD a primary key item to the cart when a 
user clicks on it.

--
You received this message because you are subscribed to the Google 
Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send 
an email to django-rest-framework+unsubscr...@googlegroups.com 
.

For more options, visit https://groups.google.com/d/optout.


--
You received this message because you are subscribed to the Google Groups "Django 
REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.