私は店舗リストと作成のためのAPIを設計するためにdjano rest frameworkを使用しています。私のアプリのコンセプトは、商人とバイヤーのつながりです。加盟店はさまざまなカテゴリの複数のストアを持つことができます。私は加盟店のモデルについて、加盟店情報、特定のカテゴリの製品のリストを含む製品、およびストアに関する情報を含むストアを持っています。ストアの作成フォームを表示します。しかし、私は新しいストアを作成しようとするとエラーが発生します。NOT NULL制約に失敗しました:stores_store.merchant_id
IntegrityErrorで/ APIは/店舗/失敗/ NOT NULL制約を作成:
私のmodels.py stores_store.merchant_id
class Merchant(models.Model):
user = models.ForeignKey(User)
phone = models.PositiveIntegerField(null=True,blank=True)
class Store(models.Model):
merchant = models.ForeignKey(Merchant)
name_of_legal_entity = models.CharField(max_length=250)
class Product(models.Model):
store = models.ForeignKey(Store)
image = models.ForeignKey('ProductImage',blank=True,null=True)
name_of_product = models.CharField(max_length=120)
class ProductImage(models.Model):
image = models.ImageField(upload_to='products/images/')
class StoreCategory(models.Model):
product = models.ForeignKey(Product,null=True, on_delete=models.CASCADE,related_name="store_category")
store_category = models.CharField(choices=STORE_CATEGORIES, default='GROCERY', max_length=10)
Serializers.py
User = get_user_model()
class UserSerializer(ModelSerializer):
class Meta:
model = User
fields = ("username","first_name","last_name","email",)
class MerchantSerializer(serializers.ModelSerializer):
user = UserSerializer()
class Meta:
model = Merchant
fields = ["id","user","phone","address","city",]
class ProductImageSerializer(ModelSerializer):
class Meta:
model = ProductImage
fields = ('id','imageName','image',)
class ProductSerializers(ModelSerializer):
image = ProductImageSerializer()
class Meta:
model = Product
fields=('id','image','name_of_product','description','price','active',)
class StoreCategorySerializer(ModelSerializer):
product = ProductSerializers()
class Meta:
model = StoreCategory
# fields=["id","store_category",]
class StoreSerializer(ModelSerializer):
# url = HyperlinkedIdentityField(view_name='stores_detail_api')
store_categories = StoreCategorySerializer(many=True)
merchant = MerchantSerializer()
class Meta:
model = Store
fields=("id",
# "url",
"merchant",
"store_categories",
"name_of_legal_entity",
"pan_number",
"registered_office_address",
"name_of_store",
"store_contact_number",
"store_long",
"store_lat",
"store_start_time",
"store_end_time",
"store_off_day",
)
class StoreCreateSerializer(ModelSerializer):
store_categories = StoreCategorySerializer()
merchant = MerchantSerializer()
class Meta:
model = Store
fields=("id",
"merchant",
"store_categories",
"name_of_legal_entity",
"pan_number",
"registered_office_address",
"name_of_store",
"store_contact_number",
"store_long",
"store_lat",
"store_start_time",
"store_end_time",
"store_off_day",
)
def create(self,validated_data):
store_categories_data = validated_data.pop('store_categories')
merchant_data = validated_data.pop('merchant')
store = Store.objects.create(**validated_data)
for store_categories in store_categories_data:
store_categories, created = StoreCategory.objects.get_or_create(pan_number=store_categories['pan_number'])
store.store_categories.add(store_categories)
for merchant in merchant_data:
merchant, created = Merchant.objects.get_or_create(user=merchant['user'])
store.merchant.add(merchant)
return store
Views.py
class StoreCreateAPIView(CreateAPIView):
queryset = Store.objects.all()
serializer_class = StoreCreateSerializer
parser_classes = (FormParser,MultiPartParser,)
マーチャント・オブジェクトは、StroreCreateSerializerに問題があるこの
"merchant": {
"id": 6,
"username": "sans",
"first_name": "sans",
"last_name": "bas",
"email": "[email protected]"
},
、オブジェクト作成の順序は次のとおりです。あなたは、最初の商人オブジェクトを作成し、作成するために、その参照を使用する必要がありますストアオブジェクト ところで、MerchantSerializer()はどこですか? –
@SijanBhandari MerchantSerializer()を追加しました。 – pythonBeginner
あなたは 'Merchant'の前に' Store'を作成しようとしていますので、あなたの 'merchant_id'はその時点でNullになります。そのため、IntegrityErrorが発生しています!あなたは 'Store'を作成する前に' Merchant'オブジェクトを作成しなければなりません。そうすれば、あなたはNOT NULL 'merchant_id'を持つことになります。 – kapilsdv