在tastypie中的相关表中插入数据

时间:2012-06-26 08:12:00

标签: python django curl tastypie

我使用了auth_user和另一个自定义模型来保存更多值

我的模型如下

class ExProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    cell_phone = models.CharField(max_length=200, blank=True)
    api_key=      models.CharField(max_length=200, blank=True)
    termination_date=models.DateField()
    picture=models.ImageField(upload_to='profile',blank=True)
    homeAddress=models.CharField(max_length=200,blank=True)
    homeNumber=models.CharField(max_length=200,blank=True)

现在我想创建一个用于注册目的的rest api,它涉及auth_user和Exprofile ...现在我面临的问题是如何在tastypie中设计一个资源,以便我可以在两个表中一起插入

资源

class UserResource(ModelResource):
        class Meta:
            queryset = User.objects.all()
            resource_name = 'user'
            excludes = ['email', 'password', 'is_active', 'is_staff', 'is_superuser']
            filtering = {
               }
            #authorization = Authorization()
            #authentication=MyAuthentication()


class ProfileResource(ModelResource):

        username = fields.CharField(attribute='user__username', readonly=True)
        class Meta:
             queryset =ExProfile.objects.select_related('User')
             resource_name = 'entry'
             #authorization = Authorization()
             #authentication = MyAuthentication()
             fields = ['username','api_key','email','homeAddress']
             filtering = {
                 'username':ALL,
                 'api_key': ALL,
                 'homeAddress': ALL,
                 'email': ALL,
                 'query': ['icontains',],
                 }

POST DATA .........

curl -v -H "Content-Type: application/json" -X POST --data '{"username":"test", "password":"123456", "entry":{"cell_phone":"somewhere","api_key":"7378237827827".....}}' http://myhost:8000/api/user/

0 个答案:

没有答案