How do add new record every time I update the screen? instead it updates the existing record.?
Here are the models.
class Dealer(models.Model):
city = models.CharField('City', max_length=30, blank=True, null=True)
contact_name = models.CharField('Contact name', max_length=250, blank=True, null=True)
dealer_id = models.CharField('dealer_id', max_length=50, primary_key=True)
dealership_name = models.CharField('Dealership name', max_length=100, blank=True, null=True)
dsr = models.CharField('DSR', max_length=50, blank=True, null=True)
email = models.EmailField('Email', blank=True, null=True)
fax = models.CharField('Fax', max_length=20, blank=True, null=True)
dealer_notes = models.TextField('notes', blank=True, null=True)
phone = models.CharField('Phone', max_length=20, blank=True, null=True)
rating = models.CharField('Rating', max_length=20, blank=True, null=True)
state = models.CharField('State', max_length=5, blank=True, null=True)
street = models.CharField('Street', max_length=60, blank=True, null=True)
zip = models.CharField('Zip', max_length=10, blank=True, null=True)
def __str__(self):
return self.dealer_id
class Activity(models.Model):
dealer_id = models.ForeignKey(Dealer, on_delete=models.CASCADE)
activity_number = models.PositiveIntegerField(default=1)
date_created = models.DateField('date created', blank=True, null=True)
created_by = models.CharField('created_by', max_length=50, blank=True, null=True)
type = models.CharField('state', max_length=50, blank=True, null=True)
activity_notes = models.TextField(max_length=500, blank=True, null=True)
def __str__(self):
return f"{self.dealer_id} {self.activity_number}"
When it lands in dealer notes page it will get the details from the user to enter model fields and save.For each record there can be many number of activity notes. But when I tried to add a new note it updates the existing one, i want it create a new record.
def create_notes(request, dealer_id):
item, _ = Activity.objects.get_or_create(dealer_id_id=dealer_id)
form = ActivityForm(instance=item)
if request.method == 'POST':
form = ActivityForm(request.POST, instance=item)
if form.is_valid():
try:
form.save()
except:
pass
else:
return redirect('dealernotes/' request.post.get('dealer_id_id'))
return render(request,
'dealernotes.html',
{'form': item,},
)
Can you help to create a new note every time? by activity number i should be able to update as well.
urls:
path('dealernotes/<str:dealer_id>?/', views.update_notes, name='dealernotes')
How do I fix this issue?
CodePudding user response:
Don't use get_or_create
and instead just use create
item = Activity.objects.create(dealer_id_id=dealer_id)
Edit 2 (scraping Edit1)
Here, Try to restructure it so that Activity is only created when you submit the form.
- Note: We're changing the template, don't forget about that
def create_notes(request, dealer_id):
if request.method == 'POST':
form = ActivityForm(request.POST)
if form.is_valid():
try:
form.save()
except:
pass
else:
# debugging error:
print(form.errors.as_data())
return redirect('dealernotes/' request.post.get('dealer_id_id'))
form = ActivityForm()
data = {
'dealer_id': dealer_id, # Pass dealer_id to the Template
'form': form,
}
return render(request, 'dealernotes.html', data)
Template
- We're going to replace the PK input with a dealer_id, that way the form is valid on the post
<form>
<input name="dealer_id" value="{{dealer_id}}"/>
/* Insert the rest of the form */
<button type="submit">Submit</button>
</form>