Home > Mobile >  How to get value from foreign kye in DJANGO?
How to get value from foreign kye in DJANGO?

Time:05-29

Hey I have 2 models in my models.py:

    username = models.CharField(max_length=250, verbose_name='username', null=True)
    refresh_token = models.CharField(max_length=300, verbose_name='refresh_token', null=True)
    id = models.CharField(max_length=150 ,primary_key=True)
    login = models.CharField(max_length=250, null=True)
    avatar = models.CharField(max_length=400, verbose_name='avatar')
    params = models.ForeignKey('Parametrs', on_delete=models.CASCADE)

class Parametrs(models.Model):
    cost_skip = models.IntegerField(null=True)
    chat_bot = models.BooleanField(default=False)

and in views.py im need to get cost_skip of user. That is TwitchUser => username => cost_skip im tried to find something in docs, but it doesnt help

CodePudding user response:

You can access it through is value:

user.params.cost_skip

CodePudding user response:

def this_view(request):
    #get your user object
    user_object = User.objects.get(pk = 1)
    
    #then you can find assined Parametrs object to it
    assined_params_object = user_object.params

    #then you can find its cost_skip
    needed_cost_skip = assined_params_object.cost_skip

    print(needed_cost_skip)
  • Related