2012-09-15 14 views
6

के साथ संबंधित संसाधनों को संयोजित करें मैं TastyPie में एकाधिक संसाधनों को कैसे जोड़ सकता हूं? मेरे पास 3 मॉडल हैं जो मैं गठबंधन करना चाहता हूं: उपयोगकर्ता, प्रोफ़ाइल और पोस्ट।TastyPie

आदर्श रूप से मुझे उपयोगकर्ता के भीतर घोंसला प्रोफाइल चाहिए। मैं UserPostResource से उपयोगकर्ता और सभी प्रोफ़ाइल स्थान दोनों का खुलासा करना चाहता हूं। मुझे यकीन नहीं है कि यहां से कहाँ जाना है। attribute kwarg, जो बारी में नियमित रूप से Django नेस्टेड देखने वाक्य रचना स्वीकार करता है में गुजर अनुमति देते हैं (जब संसाधन एक ModelResource है)

class UserProfile(models.Model): 

    user = models.OneToOneField(User) 

    website = models.CharField(max_length=50) 
    description = models.CharField(max_length=255) 
    full_name = models.CharField(max_length=50) 


class UserPost(models.Model): 
    user = models.ForeignKey(User) 

    datetime = models.DateTimeField(auto_now_add=True) 
    text = models.CharField(max_length=255, blank=True) 
    location = models.CharField(max_length=255, blank= True) 

उत्तर

10

Tastypie क्षेत्रों:

class UserResource(ModelResource): 

    class Meta: 
     queryset = User.objects.all() 
     resource_name = 'user' 
     fields = ['username','id','date_joined'] 

     #Improper Auth 
     authorization = Authorization() 

class UserProfileResource(ModelResource): 

    class Meta: 
     queryset = UserProfile.objects.all() 
     resource_name = 'profile' 


class UserPostResource(ModelResource): 
    user = fields.ForeignKey(UserResource,'user', full=True) 


    class Meta: 
     queryset = UserPost.objects.all() 
     resource_name = 'userpost' 

     #Improper Auth 
     authorization = Authorization() 

यहाँ मेरी मॉडल हैं।

तो, यह सब से पहले उपयोगी हो सकता है:

# in UserProfile model (adding related_name) 
user = models.OneToOneField(User, related_name="profile") 

और ऊपर परिवर्तन को देखते हुए निम्नलिखित:

from tastypie import fields 

class UserResource(ModelResource): 
    # ... 
    website = fields.CharField(attribute = 'profile__website') 
    description = fields.CharField(attribute = 'profile__description') 
    full_name = fields.CharField(attribute = 'profile__full_name') 
    # ... 

UserResource भीतर UserProfile मॉडल से डेटा सामने आ जाएगी।

अब UserResource के भीतर आप स्थानों की एक सूची (UserPost से आना) का खुलासा करना चाहते हैं तो आपको Tastypie विधियों में से एक को ओवरराइड करना होगा। दस्तावेज़ीकरण के मुताबिक एक अच्छा उम्मीदवार dehydrate() विधि होगा।

कुछ इस तरह काम करना चाहिए:

# in UserPost model (adding related_name) 
user = models.ForeignKey(User, related_name="posts") 

class UserResource(ModelResource): 
    # .... 
    def dehydrate(self, bundle): 
     posts = bundle.obj.posts.all() 
     bundle.data['locations'] = [post.location for post in posts] 
     return bundle 
    # ... 
+0

UserProfile उपयोगकर्ता से संबंधित है और UserPost उपयोगकर्ता से संबंधित है, तो आप इस मामले में एक उदाहरण दे सकें? – arooo

+1

यदि आप अपने मॉडल पेस्ट कर सकते हैं और कह सकते हैं कि आप किस मॉडल के 'यूजर रिसोर्स' के माध्यम से एक्सपोज़ करना चाहते हैं तो हाँ। – kgr

+0

मैंने अपना प्रश्न – arooo

संबंधित मुद्दे