How to write data to model - django

I've put together code to create/save profiles:
view.py
class ProfilePageView(FormView):
template_name = 'meal_delivery/profile.html'
form_class = ProfileForm
success_url = '/meal_delivery/profile'
def form_valid(self, form):
profiles = Profile.object.filter(user_id=self.request.user.id)
if profiles:
form.instance = profiles[0]
form.save()
messages.success(self.request, 'Profile Updated')
else:
obj = form.save(commit=False)
obj.user = self.request.user
obj.save()
messages.success(self.request, 'Profile Saved')
return super(ProfilePageView, self).form_valid(form)
def get_initial(self):
profiles = Profile.object.filter(id=self.request.user.id)
if not profiles:
return {}
return {
'first_name': profiles[0].first_name,
'last_name': profiles[0].last_name,
'address1': profiles[0].address1,
'address2': profiles[0].address2,
'city': profiles[0].city,
'state': profiles[0].state,
'zip_code': profiles[0].zip_code,
'allergens': profiles[0].allergens
}
form.py
class ProfileForm(forms.ModelForm):
first_name = forms.CharField(max_length=60)
last_name = forms.CharField(max_length=100)
address1 = forms.CharField(max_length=200)
address2 = forms.CharField(max_length=200)
city = forms.CharField(max_length=100)
state = forms.CharField(max_length=100)
zip_code = forms.CharField(max_length=25)
CHOICES = tuple((o.pk, o.name) for o in Allergen.objects.all())
allergens = forms.MultipleChoiceField(choices=CHOICES, required=False)
class Meta:
model = Profile
fields=['first_name', 'last_name', 'address1', 'address2', 'city', 'state', 'zip_code', 'allergens']
def clean(self):
pass
models.py
class Profile(models.Model):
first_name = models.CharField(max_length=60)
last_name = models.CharField(max_length=100)
address1 = models.CharField(max_length=200)
address2 = models.CharField(max_length=200)
city = models.CharField(max_length=100)
state = models.CharField(max_length=100)
zip_code = models.CharField(max_length=25)
user = models.OneToOneField(User, related_name='profile', on_delete=models.CASCADE)
allergens = models.ManyToManyField(Allergen, related_name='profiles')
object = ProfileManager()
Creates profile if one does not exist, no problem.
However, when I go to update the data, nothing updates, no errors are reported and if I check the form object, it shows the updated fields with the updated text. It just does not save the information to the dB.
The only other thing I've tried is, when updating, using obj=form.save(commit=False)....obj.save(). Of course, it didn't work either.

Related

django creating Profile with custom company fields try to conect with the owner

I have two forms (OwnerCreateForm, EmployeesCreateForm) and 3 models (Profile, Company and Owner). when the owner signs up, it creates the company and its own User object. after owner login, you can create employees.
Have the Owner connect to the profile and associate it with the company
I need to associate the owning company to the employees.
That each company manages its users, that they see the same thing
Here are the details I'm using:
MODELS
class Profile(models.Model):
user = models.OneToOneField(User, null=True, on_delete=models.CASCADE)
image = models.ImageField(default='default.jpg', upload_to='profile_pics')
def __str__(self):
return '{} Profile'.format(self.user)
class Owner(models.Model):
profile = models.ForeignKey(Profile, on_delete=models.CASCADE)
def __str__(self):
return '{} Owner'.format(self.profile)
class Tienda(models.Model):
dueƱo = models.ForeignKey(Owner, null=True, on_delete=models.CASCADE)
nombre_tienda = models.CharField(verbose_name='Nombre de la Tienda', max_length=120)
direccion = models.CharField(verbose_name='Su Direccion', max_length=160)
phone = models.CharField(max_length=11, null=True)
businessemail = models.EmailField(unique = True, verbose_name='Su email')
def __str__(self):
return self.nombre_tienda
class Employee(models.Model):
STATUS = (
('Admin', 'Admin'),
('Gerente', 'Gerente'),
('Validador', 'Validador')
)
profile = models.ForeignKey(Profile, on_delete=models.CASCADE)
role = models.CharField(choices=STATUS, max_length=16)
tienda = models.ForeignKey(Tienda, null=True, on_delete=models.CASCADE)
def __str__(self):
texto = "{0} ({1}) {2}"
return texto.format(self.tienda, self.role, self.role)
FORMS
class TiendaForm(ModelForm):
class Meta:
model = Tienda
fields = ('nombre_tienda', 'direccion', 'businessemail')
class OwnerCreateForm(UserCreationForm):
class Meta:
fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2')
model = User
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['username'].label = 'Display Name'
self.fields['email'].label = "Email Address"
class EmployeesCreateForm(UserCreationForm):
is_admin = forms.BooleanField(required=False)
is_manager = forms.BooleanField(required=False)
is_systemAdmin = forms.BooleanField(required=False)
class Meta:
fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2')
model = User
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['username'].label = 'Display Name'
self.fields['email'].label = "Email Address"
VIEWS
def registroOwner(request):
if request.method == "POST":
form = OwnerCreateForm(request.POST)
tienda = TiendaForm(request.POST)
if form.is_valid() and tienda.is_valid():
tienda.save()
form.save()
messages.success(request, f'Tu cuenta ha sido creada!')
return redirect('login')
else:
form = OwnerCreateForm()
tienda = TiendaForm()
context = {
'title': 'Sign up Owner',
'form': form,
'tienda': tienda
}
return render(request, "accounts/signup.html", context)

Required "owner_id" field is empty in the Database

I am trying to save the owner_id that is the same as the Logged In user/Authenticated user. But after I save the AddressBook form,the database saves the owner id with 'Null' value. Can someone help me knowing where am I going wrong.
models.py
class UserProfile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="profile")
mobiles = models.ManyToManyField(Product, blank=True)
class AddressBook(models.Model):
name = models.CharField(max_length=50)
owner = models.ForeignKey(UserProfile, on_delete=models.SET_NULL, null=True)
phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.")
phone_number = models.CharField(validators=[phone_regex], max_length=17) # validators should be a list
line1 = models.CharField(max_length=100)
line2 = models.CharField(max_length=100, blank=True)
city = models.CharField(max_length=25)
state = models.CharField(max_length=25)
postcode = models.CharField(max_length=8)
country = models.CharField(max_length=25)
views.py
#login_required
def address(request):
existing_order = get_user_pending_order(request)
form = AddressBookForm()
context = {
'addressbook': AddressBook.objects.all(),
'form' : form,
'order' : existing_order,
}
return render(request, 'cart/user_address.html', context)
#login_required
def process_payment(request, order_id):
if request.method == 'POST':
form = AddressBookForm(request.POST)
if form.is_valid():
form.save()
return redirect (reverse('update-records', kwargs={'order_id': order_id}))
forms.py
from django import forms
from .models import AddressBook
class AddressBookForm(forms.ModelForm):
ADDRESS_TYPE = (
('Home', 'Home'),
('Work', 'Work'),
)
address_type = forms.ChoiceField(choices=ADDRESS_TYPE, widget=forms.RadioSelect)
class Meta:
model = AddressBook
fields = ['name', 'phone_number', 'line1', 'line2', 'city', 'state', 'postcode', 'country','address_type']
Database
You can save the form with commit=False, set the owner, and then save to the database:
if request.method == 'POST':
form = AddressBookForm(request.POST)
if form.is_valid():
address = form.save(commit=False)
address.owner = request.user.profile
address.save()

How to update two models using one django form

I am facing one issue with django forms
Here is my model :
class User(models.Model):
email = models.EmailField(unique=True)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
class UserProfile(AuditFields):
user = models.ForeignKey(User, on_delete=models.CASCADE)
designation = models.CharField(max_length=200, blank=True)
contact_number = models.CharField(max_length=20, blank=True)
team = models.CharField(max_length=200, blank=True)
manager = models.CharField(max_length=200, blank=True)
joining_date = models.DateField(default=datetime.now)
I need to create a form for editing profile details of the current user
This is my form. But it is a model Form so only getting the detauls from the User Profile table only
class UserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
exclude = ['user']
How can I get first_name , last_name from User table and save it
Just add the fields as a CharField in form, and use cleaned_data attribute to fetch the data and save it:
class UserProfileForm(forms.ModelForm):
first_name = forms.CharField(max_length=30,required=True)
last_name = forms.CharField(max_length=30,required=True)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.instance:
self.fields['first_name'].initial = self.instance.user.first_name
self.fields['last_name'].initial = self.instance.user.last_name
class Meta:
model = UserProfile
exclude = ['user']
def save(self, commit=False):
instance = super().save(commit=True)
user = instance.user
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.save()
return instance

I can't update the formset

I cannot update using an inline form.
I thought it was possible before, but it wasn't.
I'm trying to solve it, but it doesn't work.
I added.
I will post any other necessary items.
"Id
This field is required.
user
A Profile with this User already exists.
"
I got an error.
#view
class UserEdit(generic.UpdateView):
model = User
form_class = forms.UserUpdateForm
template_name = 'accounts/accounts_edit.html'
success_url = reverse_lazy('person:myaccount')
def get_object(self):
return get_object_or_404(User, pk=self.request.user.user_id)
#model
class User(AbstractBaseUser, PermissionsMixin):
username_validator = UnicodeUsernameValidator()
user_id = models.UUIDField(default=uuid_lib.uuid4,
primary_key=True, editable=False)
username = models.CharField(_('username'), unique=True, max_length=50,validators=[username_validator],error_messages={
'unique': _("A user with that username already exists."),
},)
class profile(models.Model):
image = models.ImageField(upload_to='profile/',default='profile/default.jpg')
first_name = models.CharField(_('first name'), max_length=30, blank=True,null=True)
last_name = models.CharField(_('last name'), max_length=150, blank=True,null=True)
birthday = models.DateField(_('birthday',),null=True)
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,)
#form
class ProfileUpdateForm(BaseModelForm):
class Meta:
model = profile
fields = ('first_name','last_name','birthday',)
ProfileFormSet = inlineformset_factory(User,profile,form=ProfileUpdateForm,extra=0)
class UserUpdateForm(mixins.ModelFormWithFormSetMixin,BaseModelForm):
formset_class = ProfileFormSet
class Meta:
model = User
fields = ('username','email',)
#mixin
class ModelFormWithFormSetMixin:
def __init__(self, *args, **kwargs):
super(ModelFormWithFormSetMixin, self).__init__(*args, **kwargs)
self.formset = self.formset_class(
instance=self.instance,
data=self.data if self.is_bound else None,
)
def is_valid(self):
return super(ModelFormWithFormSetMixin, self).is_valid() and self.formset.is_valid()
def save(self, commit=True):
saved_instance = super(ModelFormWithFormSetMixin, self).save(commit)
self.formset.save(commit)
return saved_instance

Avoid duplicates using django ModelForm

I have the following Model, ModelForm and View:
class Clinic(models.Model):
clinicid = models.AutoField(primary_key=True, unique=True)
name = models.CharField(max_length=60)
label = models.SlugField(max_length=25)
# logo =
email = models.EmailField(max_length=50, default='')
mobile = models.CharField(max_length=15, default='')
alternate = models.CharField(max_length=15, default='', blank=True)
about = models.CharField(max_length=250, blank=True)
state = models.CharField(max_length=25)
city = models.CharField(max_length=35)
locality = models.CharField(max_length=35)
pincode = models.IntegerField(default=0)
address = models.TextField(max_length=80, default='', blank=True)
website = models.URLField(blank=True)
class ClinicMetaForm(ModelForm):
class Meta:
model = Clinic
fields = [
'name',
'label',
'email',
'mobile',
'alternate',
'about',
'state',
'city',
'locality',
'pincode',
'address',
'website'
]
unique_together = ["name", "mobile", "email"]
def newclinic(request):
if request.method == 'POST':
print('New clinic setup')
form = ClinicMetaForm(request.POST)
form.save()
msg = "Successfully saved new clinic"
print(msg)
else:
form = ClinicMetaForm()
msg=''
return render(request, 'clinic/newclinic.html', {'form': form, 'msg': msg})
The problem is that when the same data is submitted, I get duplicate entries being saved, even though I am using unique_together. Why is this happening? How can I avoid it?
You should move unique_together to model.
And if you want to add some logic in your form just overwrite validate_unique
def validate_unique(self):
#your logic here
try:
self.instance.validate_unique(exclude=exclude)
except ValidationError, e:
self._update_errors(e.message_dict)