2016-03-10 15 views
5

mô hình đầu tiên:Django Tastypie: bài giá trị trong nhiều mô hình để một đơn Tastypie Resource

class Profile(models.Model): 

    username = models.CharField(
     _('username'), 
     max_length=150, 
     unique=True, 
     help_text=_('Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.'), 
     validators=[ 
      validators.RegexValidator(
       r'^[\[email protected]+-]+$', 
       _('Enter a valid username. This value may contain only ' 
        'letters, numbers ' 'and @/./+/-/_ characters.') 
      ), 
     ], 
     error_messages={ 
      'unique': _("A user with that username already exists."), 
     }, 
    ) 
    password = models.CharField(max_length=12, default='123456') 
    first_name = models.CharField(_('first name'), max_length=30, blank=True) 
    last_name = models.CharField(_('last name'), max_length=30, blank=True) 
    email = models.EmailField(_('email address'), blank=True) 
    date_joined = models.DateTimeField(_('date joined'), default=timezone.now) 

mô hình thứ hai:

class UserEbiz(models.Model): 
    user_id = models.ForeignKey('Profile') 
    password = models.CharField(max_length=12, default='123456') 

Tastypie Resource:

class ProfileResources(ModelResource): 
    id = fields.CharField(attribute='id') 

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

     filtering = { 
      "id": ALL, 
     } 

Tôi muốn tạo một hàm có thể lưu giá trị cho cả hai bảng cùng một lúc bằng cách chỉ sử dụng một tài nguyên. Câu hỏi của tôi là làm thế nào để đăng giá trị vào các mô hình Profile và UserEbiz bằng cách sử dụng ProfileResources.

+0

Tại sao bạn không tạo tài nguyên cho mô hình UserEbiz ...! – Satyajeet

+0

Ok. Sau khi tạo tài nguyên cho mô hình UserEbiz, tôi nên làm gì cho bước tiếp theo. – nuriffah

Trả lời

1

Bạn sẽ muốn một cái gì đó như thế này:

class UserEbizResource(ModelResource): 
    user_id = fields.ToOneField(ProfileResources, attribute='user_id') 

    class Meta: 
     queryset = UserEbiz.objects.all() 
     resource_name = 'userebiz' 

     excludes = ['password'] 
     filtering = { 
      "id": ALL, 
     } 

Một số gợi ý khác:

  • Đổi tên ProfileResources-ProfileResource
  • Đổi tên UserEbiz.user_id-UserEbiz.profile
  • Xem xét sử dụng mô hình tài khoản hoặc tùy biến của Django nó thay vì một mô hình hoàn toàn tùy chỉnh
2

@ sean-hayes trả lời là tốt nhưng nếu bạn thực sự muốn Post dữ liệu trên ProfileResource thay vì UserEbiz sự thay đổi đầu tiên ProfileResource để

class ProfileResource(ModelResource): 
    ebiz = fields.ToManyField(UserEbizResource, 'userebiz_set', readonly=True) 

Thông báo Đầu tiên rằng bạn có một ForeignKey của Profile. Vì vậy, nó là 1:M cho Profile đến UserEbiz.

Thứ hai tôi đã đặt readonly thành true vì tôi sẽ tự xử lý UserEbiz dữ liệu. Tuy nhiên, nếu bạn có models.ForeignKey('Profile', blank=True, null=True) thì nó cũng giống như tạo dữ liệu M2M vì vậy, chỉ cần xóa thuộc tính readonly và bạn đã sẵn sàng.

Vì vậy, bạn có thể override tự động obj_create để tự xử lý ebiz dữ liệu. Mã có thể trông giống như

def obj_create(self, bundle, **kwargs): 
    ebiz = bundle.data.pop('ebiz', None) 
    bundle = super(ProfileResource, self).obj_create(bundle, **kwargs) 
    if not ebiz: 
     return bundle 

    ebiz.update(user_id=bundle.obj) # change it to user in model and here 
    ebiz_res = UserEbizResource() 
    ebiz_bundle = ebiz_res.build_bundle(data=ebiz) 
    ebiz_res.obj_create(ebiz_bundle) 
    return bundle 
+0

Tôi đã thử mã của bạn nhưng tôi nhận được lỗi này "error_message": "siêu (loại, obj): obj phải là một thể hiện hoặc loại phụ", – nuriffah

+0

@nuriffah bạn không chuyển thể hiện tài nguyên sang siêu lớp. Bạn có thể cập nhật câu hỏi của mình bằng cách sử dụng '' 'obj_create''' ...! – Satyajeet

+0

Tôi đã giải quyết được lỗi đó nhưng khi bài đăng đã thành công trong Profileresource nhưng không phải trong UserEbizResource..data không lưu vào mô hình UserEbiz. – nuriffah

Các vấn đề liên quan