简体   繁体   English

在Django Tastypie中从ModelResources创建对象

[英]Creating objects from ModelResources in Django Tastypie

I am unable to grasp the connection between the TastyPie ModelResource and the Django Model . 我无法掌握DeliciousPie ModelResource和Django Model之间的联系。 I have looked at the cookbook and not been able to understand this. 我看了看菜谱,却听不懂。

I have a NotificationData class where each instance belongs to a Django user (using a ForeignKey). 我有一个NotificationData类,其中每个实例都属于Django用户(使用ForeignKey)。 I want only authenticated Users to CRUD NotificationData objects, and this will be done from a native app. 我只希望通过身份验证的用户访问CRUD NotificationData对象,这将通过本机应用程序完成。 I'm using oauth2 for authentication, so you can assume that each request will be sent using an authentication header with access token. 我正在使用oauth2进行身份验证,因此可以假设每个请求都将使用带有访问令牌的身份验证标头发送。

How do I create a NotificationData object for the authenticated User using the NotificationDataResource ? 如何创建一个NotificationData使用经过验证的用户对象NotificationDataResource And how would I get the list of the NotificationData objects for that user? 我将如何获取该用户的NotificationData对象列表?

models.py: models.py:

class NotificationData(models.Model):
    user = models.ForeignKey(User)   
    notification = models.OneToOneField(Notification)
    shortcut = models.CharField(max_length=200)

api.py: api.py:

class NotificationDataResource(ModelResource):
    class Meta:
        queryset = NotificationData.objects.all()
        resource_name = 'notification_data'
        authorization = DjangoAuthorization()
        authentication = OAuth20Authentication()

Did you read the 'getting started' document? 您是否阅读了“入门”文档?

It explains what you're asking; 它解释了您的要求; you just need to set up the URL handlers 您只需要设置URL处理程序

http://django-tastypie.readthedocs.org/en/latest/tutorial.html http://django-tastypie.readthedocs.org/en/latest/tutorial.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM