Skip to content

Instantly share code, notes, and snippets.

@mcihad
Created July 19, 2023 06:49
Show Gist options
  • Save mcihad/b70705e4d5a877d16b332ea5564e9442 to your computer and use it in GitHub Desktop.
Save mcihad/b70705e4d5a877d16b332ea5564e9442 to your computer and use it in GitHub Desktop.
django choices özelliğinin veritabanından alınması
class Birim(models.Model):
kisa_ad = models.CharField(_("Kısa Ad"), max_length=50)
ad = models.CharField(_("Ad"), max_length=50)
class Meta:
verbose_name = _("Birim")
verbose_name_plural = _("Birimler")
def __str__(self):
return self.ad
def get_birim_choices():
return [(x.kisa_ad, x.ad) for x in Birim.objects.all()]
class Stok(models.Model):
birim = models.CharField(_("Birim"), max_length=50, blank=True, null=True)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._meta.get_field("birim").choices = lazy(get_birim_choices, list)()
#veya self._meta.get_field("birim").choices = get_birim_choices()
#veya self._meta.get_field("birim").choices = [(x.kisa_ad, x.ad) for x in Birim.objects.all()]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment