Get Data From Django Database Model
I have a django model,
It has two columns: phone number, and code. I want to be able to get the code if I am given a phone number. Essentially, search the table for which row has the phone number as my phone number, and fetch the code for that given row.
I could write SQL for this, but I do not know how to execute that to fetch data in django models, and I was wondering if there was a better non-sql way to do this.
class PhoneNumberVerification(models.Model): phone_number = models.TextField(max_length = 20, blank = False, unique = True) code = models.CharField(max_length = 8, blank = False)
What I want:
from .models import PhoneNumberVerification def get_code(phone_number): # do some stuff return code
def get_code(phone_number): verification = PhoneNumberVerification.objects.get(phone_number=phone_number) return verification.code
Django's documentation has great tutorial for beginners. Writing your first Django app, part 2
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module