我正在开发一个小型应用程序,用户将在其中上传Excel工作表(文件上传模型),然后在工作表中搜索与特定RegEx匹配的字符串。匹配此RegEx的字符串是另一个模型(文件设备模型)的主键。
到目前为止,我的研究指向将这种逻辑放入FileUpload模型的序列化器中,但我不确定如何实现。创建函数中的注释看起来像是我走在正确的道路上,还是应该在其他地方执行此逻辑?
模型:
class FileUpload(models.Model):
uploadid = models.AutoField(primary_key=True)
user_uploaded = models.CharField(max_length=64, blank=True)
time_uploaded = models.DateTimeField(auto_now_add=True)
file = models.FileField(upload_to=rename_uploaded_file, blank=False, validators=[validators.excel_file_extensions])
devices = models.ManyToManyField(FileDevice, blank=True, related_name='files')
class FileDevice(models.Model):
device_name = models.CharField(primary_key=True, max_length=16)
序列化器:
class FileDeviceRelationshipSerializer(serializers.ModelSerializer):
files = FileUploadSerializer(many=True, read_only=True)
class Meta:
model = models.FileDevice
fields = '__all__'
def create(self, validated_data):
# 1. Search for strings in file
# 2. Add all string matches to a set
# 3. For every entry in set, link to that instance of a different model
# 4. Save object