Created
July 19, 2023 06:49
-
-
Save mcihad/b70705e4d5a877d16b332ea5564e9442 to your computer and use it in GitHub Desktop.
django choices özelliğinin veritabanından alınması
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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