django REST combining 2 model views form for json curl
I have models:
class Emp(models.Model):
    full_name = models.CharField(max_length=100)
    mobile = models.CharField(max_length=10,blank=True,null=True)
    email = models.EmailField(blank=True,null=True)
class Enquiry(models.Model):
    date = models.DateField(default=timezone.now)
    number = models.AutoField(primary_key=True)
    products = models.ManyToManyField(Product, related_name="products") 
    referred_by_emp = models.ForeignKey(
        Emp,related_name='ref_emp',
        null=True,blank=True,
        )
The serializer classes:
class ReferrerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Emp
        fields = (
            'full_name','mobile','email',
            )
class EnquirySerializer(serializers.ModelSerializer):
    class Meta:
        model = Enquiry
        fields = (
            'name','mobile','email',
            'products',
            'referred_by_emp',
            )
I wish to get the attributes of Emp ie full_name, mobile, email in the form while entering the Enquiry details. The views for the 2 models:
class RefViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows Emp instances to be viewed or edited.
    """
    model = Emp
    queryset = Emp.objects.all()
    serializer_class = ReferrerSerializer
class EnquiryViewSet(viewsets.ModelViewSet):
        """
        API endpoint that allows Enquiry instances to be viewed or edited.
        """
        model = Enquiry
        queryset = Enquiry.objects.all()
        serializer_class = EnquirySerializer
While entering the Enquiry details in the django REST api, I wish to capture the Emp details also and submit the form. And the details should be captured in the respective models. How can this be achieved? I am new to django's REST Api and didn't find a proper way of doing it. Plz guide me with detailed code to achieve this. I tried itertools.chain, but perhaps didn't use it correctly Further I would like to invoke the curl command for the same.Thanx in advance Using django 1.6.5
好吧,这不是我推荐做的事情,但是当它刚刚发生时,我一直在那里。
class ReferrerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Emp
        fields = ('full_name','mobile','email')
class EnquirySerializer(serializers.ModelSerializer):
    class Meta:
        model = Enquiry
        fields = ('name','mobile','email','products',)
# As the fields doesn't already exist you can 'copy' them into the serializer
EnquirySerializer.base_fields["full_name"] = ReferrerSerializer().fields["full_name"]            
# Then in the view we can create another model instance.
# you should use the Serializer or a Form class and not save the data directly to a Emp instance here but i left that out for clarity.
class SomeViewSet(ModelViewSet):
    model = Enquiry
    # post() should only handle new/create calls, but if you want it more clear you can override create() instead.
    def post(self, request, *args, **kwargs):
        self.emp_data = {}
        self.emp_data["full_name"] = self.request.DATA.pop("full_name")
        return super(SomeViewSet, self).post(request, *args, **kwargs)
    def pre_save(self, obj):
       emp = Emp.objects.create(**self.emp_data)
       obj.referred_by_emp = emp
       return obj
                        链接地址: http://www.djcxy.com/p/48780.html
                        
                        
                    