in line 4 in class Video(models.model): AttributeError: module 'django.db.models' has no attribute 'model'
I think you have typo mistake. models do not have model instead it have Model. convert
Convert
Video(models.model)
to
Video(models.Model)
Answer from bkawan on Stack OverflowAttributeError: module 'django.db.models' has no attribute 'model' - Stack Overflow
Model Product in django has no attribute 'objects' - Django - Code with Mosh Forum
Django - AttributeError: 'Model' object has no attribute '***_set'
Based on your models.py, each project has a foreign key to one hole, while each hole can be used to refer to multiple projects. If you want to add a hole to a project, just do project.hole = hole.
The 'attribute_set' keyword always works in the opposite direction from how you define your foreign key -- it's for the reverse. So your Hole model has a project_set relationship, because that is the reverse relationship for the defined foreign key.
https://docs.djangoproject.com/en/2.0/topics/db/queries/#following-relationships-backward
More on reddit.comAttributeError: module 'django.db.models' has no attribute 'JSONField'
Videos
I seem to be either really stupid or having bad luck with my code.
My code can be viewed here https://github.com/niko86/django-twilio-sms. The name has nothing to do with the pypi module.
My your_view function is constantly having AttributeError: 'Project' object has no attribute 'hole_set'. The RelatedManager ***_set attributes don't seem to work.
Any help would be much appreciated.
Based on your models.py, each project has a foreign key to one hole, while each hole can be used to refer to multiple projects. If you want to add a hole to a project, just do project.hole = hole.
The 'attribute_set' keyword always works in the opposite direction from how you define your foreign key -- it's for the reverse. So your Hole model has a project_set relationship, because that is the reverse relationship for the defined foreign key.
https://docs.djangoproject.com/en/2.0/topics/db/queries/#following-relationships-backward
Thanks I’ll have a look at those.
It's called models.Model and not models.model (case sensitive). Fix your Poll model like this -
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
I also got the same error but I noticed that I had typed in Foreign*k*ey and not Foreign*K*ey,(capital K) if there is a newbie out there, check out spelling and caps.
i run into this error when trying my hands on django-shopping-cart
error
AttributeError: type object 'Grocery' has no attribute 'objects'
the error was pointing this below
return Grocery.objects.all()
Models.py
from django.db import models
from django.urls import reverse
class Grocery(models.Model):
item_name = models.CharField(max_length=50)
price = models.FloatField()
picture = models.ImageField(upload_to='pictures', default='')
def __str__(self):
return self.item_name
def get_absolute_url(self):
return reverse('marketing_sys:home')views.py
from django.shortcuts import render, redirect
from django.views.generic import DetailView
from cart.cart import Cart
from .models import Grocery
class CartAdd(DetailView):
model = Grocery
context_object_name = 'grocery'
template_name = 'mismas/cartdetail.html'
def get_queryset(self):
return Grocery.objects.all()urls.py
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from mismas.views import Grocery
app_name = 'marketing_sys'
urlpatterns =[
path('cart_add/<int:pk>/', CartAdd.as_view(), name='cart_add'),
]i will be glad to receive help. thanks
The reason for this error is that .get() returns an individual object and .update() will only operate on a QuerySet, such as what would be returned with .filter() instead of .get().
If you are using .get(), then .update() will not work. You will need to save the information to the object manually:
archivo = archivo.objects.get(archivo_id=procesar)
archivo.archivo_registros = sh.nrows
archivo.save()
You can also use update_fields if you only wish to save this particular piece of data:
archivo = archivo.objects.get(archivo_id=procesar)
archivo.archivo_registros = sh.nrows
archivo.save(update_fields=['archivo_registros'])
This prevents triggering any signals that you may not want to invoke.
Your other option is to simply use .filter().
archivo = archivo.objects.filter(archivo_id=procesar).update(archivo_registros=sh.nrows)
Note that this will update multiple objects if they exist. If you want to be sure that doesn't happen, you should include the primary key in the filter, or use one of the earlier approaches to make sure you are only modifying a single object.
Encountered this behavior and used a "filter" then update works as expected. For example:
Students.objects.select_for_update().filter(id=3).update(score = 10)
Just FYI: Unless you are handling transactions, modifying each field separately using save() might create data inconsistency in a multi-threaded environment. By the time threadA calls save() on a model, another threadB could have changed the model fields and saved. In which case threadA has to read the updated model and change.
This was on Django 1.6.2
I've been trying for a few days now to do something that I initially thought would be very simple;
From a FormView, redirect on form submission to the DetailsView for the submitted data.
After a lot of refactoring, this is where I am right now:
views.py:
class addrecipe(FormView):
form_class = AddRecipeForm
model = Recipe
template_name = 'recipebook/addrecipe.html'
fields = '__all__'
extra_context = {
'recipe_list': Recipe.objects.all()
}
def get_success_url(self):
test_recipe_id = self.object.id
return reverse('recipeBook:recipe_details', pk=test_recipe_id)forms.py:
class AddRecipeForm(forms.ModelForm):
name = forms.CharField(max_length="50", label="Recipe Name")
description = forms.Textarea(attrs={'class': 'desc-text-area'})
servings = forms.IntegerField()
tools = forms.ModelMultipleChoiceField(queryset=Tool.objects.all(), widget=forms.CheckboxSelectMultiple, required = True, help_text="Select all relevant tools")
class Meta:
model = Recipe
fields = ("__all__")urls.py:
path('<int:pk>/recipedetails', views.recipedetails.as_view(), name='recipe_details'),When submitting the data, I get the following error:
AttributeError at /recipebook/addrecipe 'addrecipe' object has no attribute 'object'
Does anyone have any idea what I need to do to get this functional? I feel like I'm losing my mind.
Your problem is here:
intention = Intention.objects.get(pk=id)
form = IntentionForm(intention) # An unbound form
The first argument to a form is the data but you are passing the instance. To properly pass the instance you should use:
intention = Intention.objects.get(pk=id)
form = IntentionForm(instance=intention) # An unbound form
The above answer is correct, however, this error can also be generated by incorrectly passing arguments in the init of a form, which is used for an admin model.
Example:
class MyForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(self, *args, **kwargs)
Notice the double passing of self? It should be:
class MyForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)