Reputation: 3762
I have two models
class LCUser(models.Model):
email = models.CharField(max_length=100,unique=True)
password = models.CharField(max_length=100)
class UserProfile(models.Model):
user = models.OneToOneField(LCUser,primary_key=True)
mobile_phone = models.IntegerField(null=True)
address = models.CharField(max_length=500,null=True)
class UserProfileResource(MultipartResource, ModelResource):
class Meta:
resource_name = 'profile'
queryset = UserProfile.objects.all()
I want to setup the /profile/ endpoint such that CRUD operations manage all 5 fields.
1) Can I do that ? 2) Is it a good practice ? 3) If not what would be my alternatives ?
Upvotes: 2
Views: 340
Reputation: 4360
You can do it like this:
class LCUser(models.Model):
email = models.CharField(max_length=100, unique=True)
password = models.CharField(max_length=100)
class UserProfile(models.Model):
user = models.OneToOneField(LCUser, primary_key=True)
mobile_phone = models.IntegerField(null=True)
address = models.CharField(max_length=500, null=True)
class LCUserResource(MultipartResource, ModelResource):
class Meta:
resource_name = 'lcuser'
queryset = LCUser.objects.all()
excludes = ('password',)
class UserProfileResource(MultipartResource, ModelResource):
user = fields.ToOneField(LCUserResource, 'user')
class Meta:
resource_name = 'profile'
queryset = UserProfile.objects.all()
Make sure to exclude the password, you don't want that getting read.
Upvotes: 1