What’s the best way to store a phone number in Django models?

I am storing a phone number in model like this: phone_number = models.CharField(max_length=12) The user would enter a phone number and I would use the phone number for SMS authentication. This application would be used globally. So I would also need a country code. Is CharField a good way to store a phone number? And, … Read more

Where’s my JSON data in my incoming Django request?

I’m trying to process incoming JSON/Ajax requests with Django/Python. request.is_ajax() is True on the request, but I have no idea where the payload is with the JSON data. request.POST.dir contains this: [‘__class__’, ‘__cmp__’, ‘__contains__’, ‘__copy__’, ‘__deepcopy__’, ‘__delattr__’, ‘__delitem__’, ‘__dict__’, ‘__doc__’, ‘__eq__’, ‘__ge__’, ‘__getattribute__’, ‘__getitem__’, ‘__gt__’, ‘__hash__’, ‘__init__’, ‘__iter__’, ‘__le__’, ‘__len__’, ‘__lt__’, ‘__module__’, ‘__ne__’, ‘__new__’, ‘__reduce__’, … Read more

How to convert a Django QuerySet to a list?

I have the following: answers = Answer.objects.filter(id__in=[answer.id for answer in answer_set.answers.all()]) then later: for i in range(len(answers)): # iterate through all existing QuestionAnswer objects for existing_question_answer in existing_question_answers: # if an answer is already associated, remove it from the # list of answers to save if answers[i].id == existing_question_answer.answer.id: answers.remove(answers[i]) # doesn’t work existing_question_answers.remove(existing_question_answer) I … Read more

How do you catch this exception?

This code is in django/db/models/fields.py It creates/defines an exception? class ReverseSingleRelatedObjectDescriptor(six.with_metaclass(RenameRelatedObjectDescriptorMethods)): # This class provides the functionality that makes the related-object # managers available as attributes on a model class, for fields that have # a single “remote” value, on the class that defines the related field. # In the example “choice.poll”, the poll attribute … Read more

Django migration strategy for renaming a model and relationship fields

I’m planning to rename several models in an existing Django project where there are many other models that have foreign key relationships to the models I would like to rename. I’m fairly certain this will require multiple migrations, but I’m not sure of the exact procedure. Let’s say I start out with the following models … Read more