[Python] How to add the OneToOneField in to the BD in Admin site that was created using...

Discussão em 'Python' iniciado por Stack, Novembro 28, 2024.

  1. Stack

    Stack Membro Participativo

    I have some classes in models.py, and after adding one more I need to add one more attribute in to the already created class, I found in internet that the best way to do this is to use setattr() function, so I made all and.. It seems work, no bugs, it is showing in the Admin site as <django.db.models.fields.related.OneToOneField> , but there are no way to chose it when I trying to create or change this field, there aren't , is it a bug or I doing smth wrong? Here is my code:

    class PlayerInstance(models.Model):
    #...
    class Competition(models.Model):
    #...
    setattr(PlayerInstance, 'choice_to_competition', models.OneToOneField(
    Competition,
    default = 'for_nothoing',
    on_delete=models.RESTRICT,
    help_text='which competition is it taking part?',
    ))


    And no, I didn't forget to add 'choice_to_competition' in to the list_display. Here 2 photos of Admin site: I made them, but I can't add them here, can't understand reason, but it is not really necessary.

    Continue reading...

Compartilhe esta Página