Note that there are some explanatory texts on larger screens.

plurals
  1. POusing tastypie to create and update models
    primarykey
    data
    text
    <p>I am using tastypie to quickly create an api for my django project. My Django consists of some Models, one of them is a customer model. The customer model has a foreign key to django's user model so it looks like this</p> <pre><code>class Customer(models.Model): ...fields.... creator = models.ForeignKey(User, related_name='created_customers' </code></pre> <p>I have created my CustomerResource</p> <pre><code>class CustomerResourse(ModelResource): creator = fields.ForeignKey(UserResource,'creator', null = True, blank= True, full=True) class Meta: queryset = Customer.objects.all() filtering = { 'last_name':ALL, 'first_name':ALL, } resource_name = 'customer' authentication = SessionAuthentication() authorization = DjangoAuthorization() </code></pre> <p>I woul like to be able to update my model using the api. Reading the tutorial it says updating a model is using PUT. So my steps were. </p> <p>1) Log in and get sessionid 2)store it 3)get the resource i need 4)modify the resource i need 5)create headers 6)use requests.put to put data back passing the headers and the sessionid cookie.</p> <p>the code(using requests)</p> <pre><code>headers = {'content-type':'application/json'} resp = requets.post(login_url, data=credentials, headers=headers) sessionid = resp.cookies['sessionid'] cookies = {'sessionid':sessionid} customer_json = request.get(customer_resource_url, cookies=cookies) customer_json['mobile'] = 'new mobile' resp.put(customer_resource_url, data=data, headers=headers, cookies=cookies) </code></pre> <p>What i get is a 401 unauthorized status code though. The user is super and staff so he can modify everything. The login happens normaly and I get the sessionid as i should. </p> <p>And a second question. If i create a new customer, will tastypie take care of foreign keys e.g taking in consideration the sessionid to determine the user creator, or does it need to be done manualy?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload