Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

361
Visualizações
Django: how to get Foreign key id?

I have 2 models as below

class Product(models.Model):
   product_name = models.CharField(max_length=100)
   product_weight = models.CharField(max_length=30)

class ProductImage(models.Model):
   product = models.ForeignKey(Product, on_delete=models.DO_NOTHING)
   image = models.ImageField(upload_to='/images/{product_id}/', blank=True)

How to extract product_id in ProductImage model?

Thanks in advance.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You can get the "raw" value of any foreign key in Django by adding "_id" to the field name

obj = ProductImage.objects.get()
obj.product_id  # Will return the id of the related product

You can also just follow the relationship but this will perform another DB lookup if the relationship has not been cached by using something like select_related

obj.product.id
over 4 years ago · Santiago Trujillo Relatório

0

Here is what I have tried so far and found the solution. The only option I found for achieving is using pre_save and post_save signals. And here is how I achieved the solution. If anyone has different solution, please share. Thanks.

from django.db.models.signals import post_save, pre_save
from django.dispatch import receiver

_UNSAVED_IMAGEFIELD = 'unsaved_imagefield'

def upload_path_handler(instance, filename):
    import os.path
    fn, ext = os.path.splitext(filename)
    return "images/{id}/{fname}".format(id=instance.product_id, 
    fname=filename)

class ProductImage(models.Model):
   product = models.ForeignKey(Product, on_delete=models.DO_NOTHING)
   image = models.ImageField(upload_to=upload_path_handler, blank=True)

@receiver(pre_save, sender=ProductImage)
def skip_saving_file(sender, instance, **kwargs):
    if not instance.pk and not hasattr(instance, _UNSAVED_IMAGEFIELD):
        setattr(instance, _UNSAVED_IMAGEFIELD, instance.image)
        instance.image = None

@receiver(post_save, sender=ProductImage)
def update_file_url(sender, instance, created, **kwargs):
    if created and hasattr(instance, _UNSAVED_IMAGEFIELD):
        instance.image = getattr(instance, _UNSAVED_IMAGEFIELD)
        instance.save()
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda