Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

python - Django Create and Save Many instances of model when another object are created

I am designing a game of chess and I would like to initialize the fields with chess figures (State model) after the start of a new ChessParty.
I read about overriding the save() model method, but I don't know how to use it in my case.
I am reading about signals like post_save, but I have the same problem.

Something like this could work?

    def save(self, *args, **kwargs):
        (Here i want create many instances of another Model)
    super(ChessParty, self).save(*args, **kwargs)

And here is my code so far:

class ChessParty(models.Model):
    chessparty_id = models.AutoField("ID partii", primary_key=True)
    arbiter = models.ForeignKey(Arbiter, related_name='sedzia', 
    verbose_name="S?dzia")
    white = models.ForeignKey(Player, related_name='Bia?e', 
    verbose_name="Bia?e figury")
    black = models.ForeignKey(Player, related_name='Czarne', 
    verbose_name="Czarne figury")
    tournament = models.ForeignKey(Tournament, verbose_name="Nazwa turnieju")

    def __str__(self):
        return "{white} vs {black}, ({tournament})"
            .format(black=self.black, white=self.white, tournament=self.tournament)


class OneMove(models.Model):
party = models.ForeignKey(ChessParty, default='0', verbose_name="Partia")
chessman = (
    ('a1_w_rook', 'bia?a wie?a a1'), ('h1_w_rook', 'bia?a wie?a h1'),
    ('b1_w_knight', 'bia?y skoczek b1'), ('g1_w_knight', 'bia?y skoczek g1'),
    ('c1_w_bishop', 'bia?y goniec c1'), ('f1_w_bishop', 'bia?y goniec f1'),
    ('d1_w_queen', 'bia?y hetman d1'), ('e1_w_king', 'bia?y król e1'),
    ('a2_w_pawn', 'bia?y pion a2'), ('b2_w_pawn', 'bia?y pion b2'),
    ('c2_w_pawn', 'bia?y pion c2'), ('d2_w_pawn', 'bia?y pion d2'),
    ('e2_w_pawn', 'bia?y pion e2'), ('f2_w_pawn', 'bia?y pion f2'),
    ('g2_w_pawn', 'bia?y pion g2'), ('h2_w_pawn', 'bia?y pion h2'),
    ('a8_b_rook', 'czarna wie?a a1'), ('h8_b_rook', 'czarna wie?a h8'),
    ('b8_b_knight', 'czarny skoczek b1'), ('g8_b_knight', 'czarny skoczek g8'),
    ('c8_b_knight', 'czarny goniec c1'), ('f8_b_bishop', 'czarny goniec f8'),
    ('d8_b_queen', 'czarny hetman d1'), ('e8_b_king', 'czarny król e8'),
    ('a7_b_pawn', 'czarny pion a7'), ('b7_b_pawn', 'czarny pion b7'),
    ('c7_b_pawn', 'czarny pion c7'), ('d7_b_pawn', 'czarny pion d7'),
    ('e7_b_pawn', 'czarny pion e7'), ('f7_b_pawn', 'czarny pion f7'),
    ('g7_b_pawn', 'czarny pion g7'), ('h7_b_pawn', 'czarny pion h7'),
 )
chessman = models.CharField(max_length=30, choices=chessman, default='pionek', verbose_name="Figura Szachowa")
mymove = []
for a, b in itertools.product('abcdefgh', '12345678'):
    name = a + b
    mymove.append((name, name))
mytuple = tuple(mymove)
move = models.CharField(max_length=2, choices=mytuple, default='a1', verbose_name="Ruch na")

class Meta:
    abstract = True


class State(OneMove):
state_id = models.PositiveIntegerField(default=0, verbose_name="numer ruchu")
is_capture = models.BooleanField(default=False, verbose_name="Czy zbita")
capture_choice = (
    ('true', 'zbity'),
    ('false', 'nie zbity'),
)
is_capture = models.CharField(max_length=9, choices=capture_choice, default='false', verbose_name="Czy zbity")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Using save():

If you want to use the save() method, you could do the following:

def save(self, *args, **kwargs):
    OtherModel.objects.create(something=kwargs['something'])
    YetAnotherModel.objects.create(
        something_else=kwargs['something_else']
    )
    super(ChessParty, self).save(*args, **kwargs)

As @e4c5 states in his comment, it is easier to implement and that is why I include it!


My take on the problem:

Although you could do this on the save(), I would recommend instead to use a signal.

Specifically use a post_save signal. Here is how to do this:

  1. Create a file your_app/signals.py:

    from django.db.models.signals import post_save
    from django.dispatch import receiver
    
    from your_app.models import ChessParty, OtherModel, YetAnotherModel
    
    
    @receiver(post_save, sender=ChessParty)
    def change_my_name_plz (sender, instance, created, **kwargs):
        if created:
            OtherModel.objects.create(something=kwargs['something'])
            YetAnotherModel.objects.create(
                something_else=kwargs['something_else']
            )
    
  2. You now need to override the ready() function on your_app/app.py:

    from django.apps import AppConfig
    
    class YourAppConfig(AppConfig):
        name = 'your_project.your_app'
    
        def ready(self):
            import your_project.your_app.signals
    
  3. Finally, add the following in your_app/__init__.py:

    default_app_config = 'your_project.your_app.apps.YourAppConfig'
    

Now you have a signal that will create an OtherModel and YetAnotherModel objects right after you create a new ChessParty object.


Alternative way to define signals:

There is an alternative way that does not use the @receiver decorator, but the connect() method:

  1. your_app/signals.py:

    from your_app.models import ChessParty, OtherModel, YetAnotherModel
    
    
    def change_my_name_plz (sender, instance, created, **kwargs):
        if created:
            OtherModel.objects.create(something=kwargs['something'])
            YetAnotherModel.objects.create(
                something_else=kwargs['something_else']
            )
    
  2. your_app/app.py:

    from django.apps import AppConfig
    from django.db.models.signals import post_save
    
    from your_app.models import ChessParty
    from your_project.your_app.signals import change_my_name_plz
    
    class YourAppConfig(AppConfig):
        name = 'your_project.your_app'
    
        def ready(self):
            post_save.connect(change_my_name_plz, sender=ChessParty)
    
  3. your_app/__init__.py stays the same as above (step 3).



与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...