django-models

how to get field type string from db model in django

邮差的信 提交于 2020-01-09 03:45:15
问题 I am doing the following: model._meta.get_field('g').get_internal_type Which returns the following: <bound method URLField.get_internal_type of <django.db.models.fields.URLField: g>> I only want the know that this field is "URLField" . How do I extract that from this output? Note: I am doing this so that I can do validation on the fields. For example if a url , I want to check if it is well formed. 回答1: If you were doing this: model._meta.get_field('g').get_internal_type() You could not

how to get field type string from db model in django

烂漫一生 提交于 2020-01-09 03:44:26
问题 I am doing the following: model._meta.get_field('g').get_internal_type Which returns the following: <bound method URLField.get_internal_type of <django.db.models.fields.URLField: g>> I only want the know that this field is "URLField" . How do I extract that from this output? Note: I am doing this so that I can do validation on the fields. For example if a url , I want to check if it is well formed. 回答1: If you were doing this: model._meta.get_field('g').get_internal_type() You could not

Why executing form.is_valid() cause deleting of the instance fields that aren't in the form in django?

一笑奈何 提交于 2020-01-07 09:24:03
问题 Suppose this view that is for edit record: def edit_post(request, slug): post = get_object_or_404(Post, slug=slug) if request.method == "POST": form = AddPostForm(request.POST, request.FILES, instance=post) # 1 if form.is_valid(): # 2 new_post = form.save(commit=False) new_post.save() return redirect('administrator:view_admin_post') ... Now assume these: I have field1 that is exist in POST model. field1 has default value and suppose that the current value of field1 depends on previous. Also

How can i update django foreignkey?

杀马特。学长 韩版系。学妹 提交于 2020-01-07 09:21:56
问题 I have a model OrderItem and it has a foreignkey order . I also have a function that creates an OrderItem like so, OrderItem.objects.create(shopping_id=_shopping_id(request), quantity=quantity, item=i, created_by=anon_user, modified_by=anon_user) As you can see i have not included order because i have failed to understand how i can be able to do it. i get the error order_items.order_id may not be NULL here is my model OrderItem class OrderItem(SmartModel): shopping_id = models.CharField(max

Django Model Form Many to Many field is not displaying value

巧了我就是萌 提交于 2020-01-07 09:05:32
问题 I have a many to many field and a foreign key field on a model form. It appears to be making the right query but the result are Objects and not the values of the object. Do I need to overwrite the queryset using a VALUES_LIST method? forms.py class Meta: model = AccountParameters fields =['acctFilterName', 'excludeClassification', 'tradingCash',] #exclude = ['acctFilterName'] labels = { 'acctFilterName': _('Account Filters:'), 'excludeClassification': _('Exclude Classifications: '),

Copying Django model row data before updating

纵然是瞬间 提交于 2020-01-07 05:35:26
问题 Before updating a row, I'd like to save its current results into another table. Currently I am using pre_save but it does not seem to work as intended. It gives me updated data, not pre-updated data. class Country(models.Model): name = models.CharField(max_length=16) def make_copy(sender, **kwargs): obj = kwargs['instance'] pre_save.connect(make_copy, sender=Country) If I change the country name from USA to Australia, for example, the obj.name in pdb will display the post-updated name of

Django model Meta option unique_together for appengine

狂风中的少年 提交于 2020-01-07 04:53:04
问题 I have an app on appengine that stores some data fields entered by user. I want to prevent redundant entries, i.e. if all fields are same, data should not be entered in database. (Optional) If identical data is entered, value of a corresponding column "count" should be incremented. I tried using Django Meta option unique_together for this purpose but it doesn't seem to work. Identical data is still being stored in database. Please help. Here is my code: class Log(db.Model): name = db

Changing name of Foreign Key items at admin page in Django

僤鯓⒐⒋嵵緔 提交于 2020-01-07 04:34:12
问题 I have created two Django models, were a field from one model is a Foreign key to another (as per below). class Units(models.Model): name = models.CharField(max_length=10, primary_key=True) symbol = models.CharField(max_length=5) class Targets(models.Model): name = models.CharField(max_length=100) unit = models.ForeignKey(MicroNutrientUnits) ... These models are then registered to the admin site via admin.site.register . When adding an item to table Target unit correctly reflects items from

Django - Proper exception for ContentType.get_object_for_this_type()

a 夏天 提交于 2020-01-06 21:58:30
问题 I am learning to use the ContentType framework and need some help raising an exception for get_object_for_this_type() . According to the code: Returns an object of this type for the keyword arguments given. Basically, this is a proxy around this object_type's get_object() model method. The ObjectNotExist exception, if thrown, will not be caught, so code that calls this method should catch it. I am wondering if there is a way to do this without bringing in the actual models being referenced.

Unable to link Blog Posts to Specific Category Django

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-06 20:16:50
问题 I used Tango with Django to create a database containing categories, and I used the Django Girls tutorial to add a blog to the database. Both are working fine, but I have been having trouble linking each blog post to its respective category. Right now, all posts go to my post_list.html page. If I were doing this from scratch, I would add a new view, add a new template, add a url mapping, and then add a link from the category page. I also am aware that I need to edit my blog post model to