Please tell me how to register new data obtained by scraping in db with django.
I understand that I create a lot of model instances in a loop and finally register them using bulk_create (), but there are 7 models.
They are connected by foreign key and its related_name respectively.
I don’t know how to do this
Should I find a model (tentatively called A) that is atmospherically central and pass an instance of another model to A’s foreign key?
What should I do if I want to pass more than one?
And how do I register from instance object A to C when A is referencing model B and B is referencing C?
below is the models.
I supposed that Project is A model and Municipalities is B, Prefecture is C.
from django.db import models
# Create your models here.
#
class Prefecture(models.Model):
name=models.CharField("prefecture",max_length=10)
#
class Municipalities(models.Model):
prefecture = models.ForeignKey(Prefecture, on_delete=models.CASCADE, related_name='municipalities')
name=models.CharField("municipality",max_length=10)
#
class Client(models.Model):
prefecture = models.ForeignKey(Prefecture, on_delete=models.CASCADE, related_name='client',null=True,blank=True)
municipalities = models.ForeignKey(Municipalities, on_delete=models.CASCADE, related_name='client',null=True,blank=True)
department = models.CharField("aaa",max_length=100)
#
class Project(models.Model):
name = models.CharField("name",max_length=100)
serial_no = models.CharField("serial_no",max_length=100,null=True,blank=True)
BID_METHOD_CHOICES = (
(0, 'bbb'),
(1, 'hhh')
)
bid_method_type = models.IntegerField("bid_method",choices=BID_METHOD_CHOICES,default=0)
BID_FORMAT_CHOICES = (
(0, 'jjj'),
(1, 'www'),
(2, 'mmm'),
(3, 'ggg'),
)
bid_format_type = models.IntegerField("bid_format_type",choices=BID_FORMAT_CHOICES)
CATEGORY_CHOICES = (
(0, 'aaa'),
(1, 'bbb'),
(2, 'ccc'),
(3, 'ddd'),
(4, 'eee'),
)
category_type = models.IntegerField("category_type",choices=CATEGORY_CHOICES)
SECTOR_CHOICES = (
(0, 'aaa'),
(1, 'bbb'),
(2, 'ccc'),
(3, 'ddd'),
(4, 'eee'),
)
sector_type = models.IntegerField("sector_type",choices=SECTOR_CHOICES)
place = models.CharField("place",max_length=100,null=True,blank=True)
client = models.ForeignKey(Client, on_delete=models.CASCADE, related_name='project')
# etc...
#
class AttachedFile(models.Model):
project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='attach_file')
CATEGORY_CHOICES = (
(0, 'hhh'),
(1, 'vvv'),
(2, 'qqq'),
(3, 'xxx'),
)
category_type = models.IntegerField("category_type",choices=CATEGORY_CHOICES)
path = models.CharField("path", max_length=255)
#
class Bidder(models.Model):
name = models.CharField("name",max_length=100)
prefecture = models.ForeignKey(Prefecture, on_delete=models.CASCADE, related_name='bidder',null=True,blank=True)
municipalities = models.ForeignKey(Municipalities, on_delete=models.CASCADE, related_name='bidder',null=True,blank=True)
# etc...
#
class BidResult(models.Model):
project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='bid_result')
bidder = models.ForeignKey(Bidder, on_delete=models.CASCADE, related_name='bid_result')
RESULT_CHOICES = (
(0, 'b'),
(1, 'y'),
(2, 'h'),
)
result_type = models.IntegerField("result_type",choices=RESULT_CHOICES,blank=True,null=True)