This is a question about ForeignKey in django.

Asked 2 years ago, Updated 2 years ago, 44 views

1) What should I do if I want to default one of the data in the table set with ForeignKey?

class Condition (models.Model): #StatusNameEx) Application/Scheduled/Processing/Completed
    state_name=models.CharField(max_length=30)
    def  __str__(self):
        return self.state_name

class Apply(models.Model):
     condition=models.ForeignKey(Condition) 

We are planning to create data for application/scheduled/processed/completed in the Condition table. We will prevent the user from seeing from. (Only the administrator will check)

2) This is the last time. The Register class includes user, company, and office models. I'm going to log in and write. Then, when I write, I want to include company and office data in the article from my logged-in information.

What I thought was that in the Apply application class, if you put the register in a foreign key and write, it will automatically be used You tried to include your information. However, when I wrote it, it was written that I had to choose from the data stored by all members in the user's writing form, so I asked because I wanted to change it to be automatically saved.

class register(models.Model): #membership form
    user=models.OneToOneField(User,on_delete=models.CASCADE) #id/PW/Email
    company=models.ForeignKey(Company) #CompanyName
    office=models.CharField(max_length=30) #Branch and Department

   def __str__(self):
            return self.Title+" "+self.office

class Apply(models.Model):
    register=models.ForeignKey(Register,null=True) #Automatically saves member information
    username_equipment=models.CharField(max_length=30)

You will see a screen where you select your team, such as the screen above. I hope the information of the logged-in user will be automatically included.

Conclusion If you set the register's Foreign Key in the Apply class, you will select the user, and I hope that the register value of the user who automatically accessed it will be included.

django

2022-09-22 15:31

1 Answers

(1) It is strange to create a model (table) called Condition to store only the state. Save it using IntegerField if you don't need any other reasons. If you declare the condition field as below and use the choice option, you can select the option in the admin. You can put the default value in the desired value.

condition = models.IntegerField (default=0, choices=[(0, "Application"), (1, "Scheduled", (2, "Processing"), (3, "Full")))

(2) Looking at the content you posted last time , when you input Apply, you used ModelForm to limit fields, but is there still a screen for selecting user information?


2022-09-22 15:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.